convert timestamp to date, then convert back to timestamp
My server app returns a timestamp string which is in UTC.
Now I want to convert it to a date object to get the local datetime, then convert that date object back to timestamp to get the local timestamp.
This doesn't seem to work as both strings outputted are identical
console.log(JSON.stringify(timestamp));
var date = new Date(timestamp*1000).getTime();
console.log(JSON.stringify(date));
How do I work this out?
You need to create a new instance of Date using the timestamp (which I assume is already a well-formatted date-object). Your code will look like this:
console.log(JSON.stringify(timestamp));
var date = new Date(timestamp);
console.log(JSON.stringify(date));
Give it a try and let me know if the output is still the same.
I think this is what u need. You need to take the current time, convert it and send it back to the server in the format u need.
var timestamp = 1470621520;
console.log(JSON.stringify(timestamp));
var date = Math.floor((new Date()).getTime() / 1000);
console.log(JSON.stringify(date));
EDIT As per the comments, for converting the UTC time to local timezone you could do,
var utcDate = 1470621520;
var localDate = new Date(utcDate);
console.log(localDate);
This will automatically take care of the UTC to local time zone conversion.
链接地址: http://www.djcxy.com/p/8146.html上一篇: 将json结果转换为日期
下一篇: 将时间戳转换为日期,然后转换回时间戳