参数列表Google App Engine,Jinja 2

时间:2016-08-08 17:03:59

标签: javascript html

这是我的time.js文件:

function timeSince(date) {
    var seconds = Math.floor((new Date() - date) / 1000);
    var interval = Math.floor(seconds / 31536000);
    var timesince = "";
    if (interval > 1) {
        timesince = interval + " years";
    }
    interval = Math.floor(seconds / 2592000);
    if (interval > 1) {
        timesince = interval + " months";
    }
    interval = Math.floor(seconds / 86400);
    if (interval > 1) {
        timesince = interval + " days";
    }
    interval = Math.floor(seconds / 3600);
    if (interval > 1) {
        timesince = interval + " hours";
    }
    interval = Math.floor(seconds / 60);
    if (interval > 1) {
        timesince = interval + " minutes";
    }
    timesince = Math.floor(seconds) + " seconds";
    document.write(timesince)
}

这是我的html片段:

<html>
    <head>
        <title>A Simple Blog</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="/static/js/time.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="/static/css/bootstrap.min.css">
        <link rel="stylesheet" href="/static/css/style.css">
    </head>
        <body>


<div class="post-heading">
    <h1>{{post.title}}</h1>
    <br><br>
    <!-- <h2 class="subheading"></h2> -->
    <span class="meta">Posted by <a href="#">{{post.author.username}}</a> ago.</span>
    <script>
        timeSince({{post.created}});
    </script>
</div>

我似乎无法找出为什么我一直收到这个错误。 我正在使用谷歌应用程序引擎与python和jinja2来构建这个应用程序,但我猜它是我的js代码的格式,也许导致这个问题?任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

所以我发现pyhton中的日期对象与javascript中的日期对象不同,所以我的代码首先不起作用。另外{{post.created}}实际上并没有返回日期对象,我相信如果我错了,它会返回一个正确的字符串。