have problem for format date in JavaScript, this is my function code
//originalDate = '2016-03-02 09:12:14.989522';
var d = new Date(originalDate),
month = d.getMonth() + 1,
day =d.getDate(),
year = d.getFullYear(),
hour = d.getHours(),
min = d.getMinutes();
alert([day, month, year].join('-')+' '+[hour,min].join(':'));
and my original date ='2016-03-02 09:12:14.989522';
and my code always return 'Nan-Nan-Nan Nan:Nan'
, It's seen unknown originalDate that I pass to.
any help?
Note: datatype in database of date of mine is timestamp
Best Answer
Simple solution: Replace the space in your
date
string with a "T".(However, to be completely technically correct, you should also include a time zone indicator at the end, either an additional "Z" to indicate UTC, i.e. Coordinated Universal Time, or "+hh:mm" or "-hh:mm" to indicate a time zone offset.)
The MDN site for
Date.parse()
writes:and
The ISP 8601 specs referred to above writes:
and
Here is your code re-written replacing the space in
date
with a "T". If that doesn't work in your browser, add a "Z" or time zone offset at the end ofdate
.