0

Im trying to make something like, time after last change.

    <script language="javascript">
        var x = new Date(Date.parse(document.lastModified));
        document.write(x - document.lastModified);
    </script>

I've got this code but Im getting NaN instead of time. What's wrong?

1 Answers1

0

Try to think about solving the problem by evaluating the code yourself. Here's what this evaluates to in a 'frame by frame' view:

1

     var x = new Date(Date.parse(document.lastModified));
    document.write(x - document.lastModified);

2

     var x = new Date(Date.parse("11/29/2013 14:37:21"));
    document.write(x - document.lastModified);

3

    var x = new Date(1385753841000);
    document.write(x - document.lastModified);

4

    var x = "Fri Nov 29 2013 14:37:21 GMT-0500 (EST)";
    document.write(x - document.lastModified);

5

    document.write("Fri Nov 29 2013 14:37:21 GMT-0500 (EST)" - "11/29/2013 14:37:21");

6

    document.write(NaN);

And you get a NaN because string minus string === NaN.

Frame 3 where you parse the date returns a UNIX Epoch timestamp (number of seconds since 1970 midnight GMT). You can subtract two epochs to get a difference of seconds.

Incognito
  • 20,537
  • 15
  • 80
  • 120