admin管理员组文章数量:1323714
How to convert 1304921325178.3193
to yyyy-mm-dd
-> in javascript?
I use highchart and I would like to convert data(xAxis[0]0)
to yyyy-mm-dd
.
I tried to parse the millisecond using this function
function(valTime) {
var date = new Date(valTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
m = (m < 10) ? '0' + m : m;
d = (d < 10) ? '0' + d : d;
return [y, m, d].join('-');
}
However, there is a gap between actual date(2015-01-26) and selected date in the chart (2015-01-29). captured image I guess if I calculate .3193, the date will be matched.
Is there any way to get the right date from the millisecond?
How to convert 1304921325178.3193
to yyyy-mm-dd
-> in javascript?
I use highchart and I would like to convert data(xAxis[0]0)
to yyyy-mm-dd
.
I tried to parse the millisecond using this function
function(valTime) {
var date = new Date(valTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
m = (m < 10) ? '0' + m : m;
d = (d < 10) ? '0' + d : d;
return [y, m, d].join('-');
}
However, there is a gap between actual date(2015-01-26) and selected date in the chart (2015-01-29). captured image I guess if I calculate .3193, the date will be matched.
Is there any way to get the right date from the millisecond?
Share Improve this question edited Aug 23, 2021 at 21:57 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Mar 6, 2018 at 16:15 GilGil 1252 gold badges5 silver badges13 bronze badges 2- 1 Possible duplicate of Converting milliseconds to a date (jQuery/JS) – sliptype Commented Mar 6, 2018 at 16:17
-
1
You can convert millisecond to date by
new Date(millisecond)
. But this isDate
. You might want to see momentjs. to display the Date in your preferred format. – ntalbs Commented Mar 7, 2018 at 12:04
1 Answer
Reset to default 5Your ms are actually pointing to 2011-05-09T06:08:45.178Z:
var date = new Date(1304921325178.3193); // Date 2011-05-09T06:08:45.178Z
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
console.log(`${year}-${month}-${day}`); // 2011-05-09
本文标签: How to convert millisecond(13049213251783193) to 39yyyymmdd39 in javascriptStack Overflow
版权声明:本文标题:How to convert millisecond(1304921325178.3193) to 'yyyy-mm-dd' in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129403a2422089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论