cup_of
cup_of

Reputation: 6687

Loop through json and change format of date

Hello I have some data stored in a variable like so:

var myData = {
    "results": [
        {
            "time": "20040203",
            "count": 2
        },
        {
            "time": "20040205",
            "count": 1
        },
        {
            "time": "20040206",
            "count": 1
        },
        {
            "time": "20040209",
            "count": 2
        },
        {
            "time": "20040210",
            "count": 2
        },
        {
            "time": "20040211",
            "count": 4
        }
    ]
}

The function I am writing needs the date to be in the format that Date.parse() uses for example Tue Jan 01 2013 16:00:00 GMT-0800 (PST)

I also think that the format 2003-01-01 will work also if that is easier.

Is this possible?, if so can anyone shoot me example code or point me in the right direction?

Thanks!

Upvotes: 0

Views: 1112

Answers (4)

Jonathan
Jonathan

Reputation: 792

The following code will parse a string in format "YYYYMMDD" in the corresponding date object. Calling Date#toString() will print it like Tue Feb 03 2004 00:00:00 followed by your timezone.

You may need to take a look at the Date's documentation.

var parseDate = function (str) {
  var year = parseInt(str.substr(0, 4), 10);
  var month = parseInt(str.substr(4, 2), 10) - 1;
  var day = parseInt(str.substr(6, 2), 10);
  return new Date(year, month, day);
}

For example:

var date = parseDate('20040203');
console.log(date.toString());
// prints "Tue Feb 03 2004 00:00:00 GMT+0100 (CET)"

So may use parseDate function in your data set:

myData.results.forEach(result => {
  result.date = parseDate(result.time);
});

Upvotes: 1

Ben Fried
Ben Fried

Reputation: 2204

Your "time" strings are very odd, but I'm assuming "20040203" Is 2004-02-03 or February 3rd, 2004?

You can easily write a function to split those numbers up and format them, or you could use a really nice library like https://momentjs.com/

With moment it would be as simple as:

myData.results.forEach(result => {
  result.time = moment(result.time, 'YYYYMMDD').toDate(); 
  // or use .format('MM-DD-YYYY') or whatever format you like instead of .toDate()
});

Upvotes: 2

trincot
trincot

Reputation: 349946

You can inject the hyphens to get from a YYYYMMDD to a YYYY-MM-DD format using a replace and a regular expression:

const myData = {
    "results": [
        {
            "time": "20040203",
            "count": 2
        },
        {
            "time": "20040205",
            "count": 1
        },
        {
            "time": "20040206",
            "count": 1
        },
        {
            "time": "20040209",
            "count": 2
        },
        {
            "time": "20040210",
            "count": 2
        },
        {
            "time": "20040211",
            "count": 4
        }
    ]
};

myData.results.forEach(result => {
    result.time = result.time.replace(/(....)(..)(..)/, "$1-$2-$3");
});

console.log(myData);

Upvotes: 2

Gavin
Gavin

Reputation: 4515

const myData = {
    "results": [
        {
            "time": "20040203",
            "count": 2
        },
        {
            "time": "20040205",
            "count": 1
        },
        {
            "time": "20040206",
            "count": 1
        },
        {
            "time": "20040209",
            "count": 2
        },
        {
            "time": "20040210",
            "count": 2
        },
        {
            "time": "20040211",
            "count": 4
        }
    ]
};

myData.results.forEach(result => {
  if (parseInt(result.time))
    result.time = new Date(parseInt(result.time)).toString();
});

console.log(myData);

Upvotes: 3

Related Questions