tags:

views:

59

answers:

3

Hi,

I'm using django currently and outputting the date in seconds from the unix epoch. How do I use jquery time ago with unix epoch?

I see this example: January 10, 2015

<abbr class="timeago" title="2015-01-10T15:00:00Z">January 10, 2015</abbr>

but can i do something like:

<abbr class="timeago" title="2015-01-10T15:00:00Z">{{UNIX_EPOCH_IN_SECONDS}}</abbr>

Thanks!

+1  A: 

You can initialize a Date object with a unix timestamp, but Javascript's date expects time in milliseconds, so it's simply:

var d = new Date(<?php echo date('U') ?>000);

which turns into something like:

var d = new Date(1285027311000);

It'll also parse most standard textual date formats, must as PHP's strtotime() will, though you'll have to test exactly how forgiving it is.

Marc B
@Marc B: Maybe I missed your point but the question was about jQuery and django?
Michael Mao
+1  A: 

The Date() constructor can take the number of milliseconds since 00:00:00 UTC 1/1/1970, and timeago can be used with a Date object. So:

jQuery.timeago(new Date(unixSeconds * 1000));

Should work.

C-Mo
A: 

Thanks for everyone's answer.

For django/python, Does anyone know how to output in in 8601 time format. I want to preserve the timezone so my server timezone has no effect on the client time

David Hsu