0

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

Ramare
  • 33
  • 4
  • 3
    `new Date(1503575274000)` should do it. That is not strange format, its called unix timestamp – abhishekkannojia Aug 24 '17 at 12:24
  • 3
    Possible duplicate of [How do I get a human readable date from a unix timestamp in Javascript?](https://stackoverflow.com/questions/7827617/how-do-i-get-a-human-readable-date-from-a-unix-timestamp-in-javascript) –  Aug 24 '17 at 12:25
  • it's would be nicer to have your date like this : `2017-08-24T18:25:43.511Z` into your json, it will be automatically converted to javascript Date object – Olivier Boissé Aug 24 '17 at 12:25

3 Answers3

0

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"
rath
  • 3,655
  • 1
  • 40
  • 53
0

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.

Nico Bleiler
  • 475
  • 4
  • 14
0

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>
nersoh
  • 186
  • 1
  • 7