Django timestamp to Javascript timestamp

2,597 views
Skip to first unread message

Matt Thompson

unread,
Nov 29, 2010, 1:16:01 PM11/29/10
to Django users
Hello guys and girls,

I'm looking to use Flot to graph some data I have in my Django app.
I'd like to graph a time based query and need to get the time is
Javascript format (milliseconds since 01/01/1970).

I can get it into seconds\unix format, this is seconds since
01/01/1970, but not milliseconds.

Can anyone help?

Thanks,
Matt



Bill Freeman

unread,
Nov 29, 2010, 8:47:15 PM11/29/10
to django...@googlegroups.com
Unix (and python) timestamps are also from the beginning of Jan 1, 1970, but are
in seconds, so you might think that multiplying by 1000 would be enough. But
unix timestamps (and, presumably, their emulation on Windows) are in GMT, while
JavaScript, if memory serves, uses the browser's local machine's timezone. JS
data/time manipulations have enough stuff to fix this, but you have to
work a bit,
including considering whether you are in daylight savings time, etc..

Bill

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Brett Thomas

unread,
Nov 29, 2010, 9:26:31 PM11/29/10
to django...@googlegroups.com
I found it easiest to use the javascript date constructor with a date string. Just have Django output the date in whatever time zone you want it displayed to the user:
var d=new Date("November 29, 2010 9:22:00");
Unfortunately Django's default Date display, the ISO standard, prints a string that cannot be read by javascript, so:
var d=new Date("{{ mymodel.date }}");
will not work out of the box.

If you change the DATETIME_FORMAT setting to "r", some other standard, the above will work.

Also...you are using a DateTimeField, not a DateField, right?

sacabuche

unread,
Nov 30, 2010, 11:11:58 AM11/30/10
to Django users
You can use this format

{{date|date:"Y/m/d H:i:s"}}
d = new Date("2010/11/30 09:58:25");

The problem with ms is that Javascripts accepts only two decimals so
if you want to use miliseconds you can do something like this:

function to_datetime(datetime){
// from python >>> datetime.now().strftime("%d/%m/%Y %H:%M:%S.%f")
//transform datetime= "dd/mm/YYYY HH:MM:SS.ms" string to Date()
try{
datetime.getTime();}
catch(err){

var d_t = datetime.split(/[/\s:.]/);
var dmyHMS = new Array;
for(var i in d_t){
dmyHMS.push(parseFloat(d_t[i]));
}
var datetime = new Date(dmyHMS[2],
dmyHMS[1]-1,
dmyHMS[0],
dmyHMS[3],
dmyHMS[4],
dmyHMS[5],
dmyHMS[6]) ; <= you can truncate to two
decimals from python (%.2f) %value
}
return datetime


Now i'm working in a graph time based in canvas, tell me if you are
interested, and i'll send you the file

sacabuche

unread,
Nov 30, 2010, 11:19:45 AM11/30/10
to Django users
I forgot to get miliseconds you just have to do:

datetime.getTime();

Matt Thompson

unread,
Nov 30, 2010, 1:14:12 PM11/30/10
to Django users
Thanks for the info guys, after I slept on it I went a different way.
Used a model manager that added a column to the row (p.js_timestamp =
time.mktime(p.datetime.timetuple()) * 1000)

Matt
Reply all
Reply to author
Forward
0 new messages