Does anyone know how I can take a mysql datetime stamp, such as YYYY-MM-DD HH:MM:SS and either parse it or convert it to work in JavaScript's Date() function, such as.. Date('YYYY, MM, DD, HH, MM, SS);
Thank you!
Does anyone know how I can take a mysql datetime stamp, such as YYYY-MM-DD HH:MM:SS and either parse it or convert it to work in JavaScript's Date() function, such as.. Date('YYYY, MM, DD, HH, MM, SS);
Thank you!
A quick search in google provided this:
function mysqlTimeStampToDate(timestamp) {
//function parses mysql datetime string and returns javascript Date object
//input has to be in this format: 2007-06-05 15:26:02
var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
Assuming that mysql_date
contains the date in MySQL format:
var js_date=new Date(mysql_date.replace(/[-: ]/g,', '))
are you in php?
var dateString = <? echo "'2000-09-10 00:00:00';"; ?>
var myDate = new Date(dateString);
Some of the answers given here are either overcomplicated or just will not work (at least, not in all browsers). If you take a step back, you can see that the MySQL timestamp has each component of time in the same order as the arguments required by the Date()
constructor.
All that's needed is a very simple split on the string:
// Split timestamp into [ Y, M, D, h, m, s ]
var t = "2010-06-09 13:12:01".split(/[- :]/);
// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
alert(d);
// -> Wed Jun 09 2010 13:12:01 GMT+0100 (GMT Daylight Time)