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 is Date. You might want to see momentjs. to display the Date in your preferred format. – ntalbs Commented Mar 7, 2018 at 12:04
Add a ment  | 

1 Answer 1

Reset to default 5

Your 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