Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create JavaScript Date object from date string

Tags:

javascript

I would like to create a valid JavaScript Date object from the following string "010-10-25T23:25:55.847Z".

This string comes out of a PostGIS database "timestamp with time zone" Data Type field.

Anyone know how i can do this?

Fail: *Edit:*Sorry, i had bad date string:

var startDate = new Date("2010-10-30T14:10:42.377Z");

EDIT #2: This works fine now with the RIGHT date string.... sorry.

var startDate = new Date("2010-10-30T14:10:42.377Z");
like image 388
capdragon Avatar asked Aug 12 '11 15:08

capdragon


2 Answers

These links might help:

Javascript equivalent of php's strtotime()?

http://phpjs.org/functions/strtotime:554

like image 128
koosa Avatar answered Nov 15 '22 05:11

koosa


I'm not sure what that format it, but this will give you each number:

var results = "010-10-25T23:25:55.847Z".match(/\d+\.{0,1}\d+/g);
var year = results[0]; // maybe ?
var month = results[1];
var day = results[2];
var etc...;
new Date(year, --month, day, hour, minutes, seconds);

or if it's kinda like UTC,

new Date(Date.UTC.apply(this, "010-10-25T23:25:55.847Z".match(/\d+\.{0,1}\d+/g)))
like image 31
Joe Avatar answered Nov 15 '22 05:11

Joe