简体   繁体   中英

Converting Unix Date format

I have a JSON that keeps DateTime values in unix view.

How do I convert it to human readable value using JavaScript? For example:

1503575274000

to 2017-08-24 15:11:54

I am using it to build charts using google visualization, my code looks like this:

function drawChart() {
var jsonData = $.ajax({
    url: "jsonUrl",
    type: "GET",
    dataType: 'json',
    async: "false"
}).done(function (jsonData) {
    var data = new google.visualization.DataTable();
    data.addColumn('number', 'dateExecutes');
    data.addColumn('number', 'passed');
    data.addColumn('number', 'failed');

    jsonData.forEach(function (row) {
        data.addRow([
            row.dateExecutes,
            row.passed,
            row.failed,
        ]);
    });

I also need to use regular date format instead of DateExecutes

That's a UNIX timestamp. You just pass it to Date like so:

var d = new Date(1503575274000);
d.toISOString(); // "2017-08-24T11:47:54.000Z"

This is the unix timecode, as stated in the comments you can convert it like this:

new Date(1503575274000)

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.

To manipulate Date/Time, I would recommend using Moment.js https://momentjs.com/docs/#/parsing/

 var dateTimeStr = moment(1503575274000).format("Y-MM-DD HH:mm:ss"); console.log(dateTimeStr); 
 <script src="https://momentjs.com/downloads/moment.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM