0

This is my time.js file:

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)
}

and this is my html snippet:

<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>

I can't seem to find out why I keep getting this error. I'm using Google app engine with python and jinja2 to build this app but I'm guessing it's the format of my js code that is causing this problem perhaps? Any advice would be appreciated.

Steven Kwok
  • 199
  • 4
  • 15
  • 1
    `{{post.created}}` is probably creating something funny. Can you show us the generated code from that statement? – Mike Cluck Aug 08 '16 at 17:07
  • @Mike C {{}} is for inserting variables to the html with Jinja 2 framework. I tried substituting {{post.created}} with a date object and the error is still there. I'm not getting much back from the console either. It just says "Uncaught SyntaxError: missing ) after argument list". – Steven Kwok Aug 08 '16 at 17:14
  • I know what it does, that's why I want to see what the actual generated HTML looks like. I'd still like to see what the generated HTML looks like, if you don't mind. In the error in the console, it should point you to a line number. What line number does it say the error is occurring at? – Mike Cluck Aug 08 '16 at 17:24
  • `` The console points to this line. – Steven Kwok Aug 08 '16 at 17:27
  • Yup, just what I thought. That isn't a valid string or date object. Try `timeSince('{{post.created}}')`. (notice the quotes marks) – Mike Cluck Aug 08 '16 at 17:33
  • now this is what I get: `` Uncaught ReferenceError: timeSince is not defined from the console – Steven Kwok Aug 08 '16 at 18:16
  • Well, the error message is pretty self-explanatory. You don't have a function named `timeSince`. – Mike Cluck Aug 08 '16 at 20:02
  • @MikeC yea so I found out that the date object in pyhton is difference from the date object in javascript so my code will not work in the first place. Also {{post.created}} doesn't actually return a date object, i believe it return a string correct me if I'm wrong. So I wrote a pyhton function to get the desired result instead. Thanks for the help anyway. – Steven Kwok Aug 08 '16 at 20:14
  • You are correct. You may want to read through [this post](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) to help clear things up. Completing that bridge between Python and Javascript can get a little confusing. – Mike Cluck Aug 08 '16 at 20:30

1 Answers1

0

so I found out that the date object in pyhton is difference from the date object in javascript so my code will not work in the first place. Also {{post.created}} doesn't actually return a date object, i believe it return a string correct me if I'm wrong.

Steven Kwok
  • 199
  • 4
  • 15