0

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!

4
  • 6
    Yes it is. Next question! Commented Aug 25, 2017 at 18:52
  • Do you know what time zone the dates should be in? There isn't enough information in the data to know that it should be -0800. Commented Aug 25, 2017 at 18:53
  • Those "time" values don't resemble JavaScript time values, unless they are all dates from around 1969 or so. Try this in the dev tools JavaScript console for example and see what it prints: new Date(20040211) Commented Aug 25, 2017 at 18:56
  • I suppose the time property uses the YYYYMMDD format. Commented Aug 25, 2017 at 19:04

4 Answers 4

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

hey gavin thanks for your answer, this spits out the data in correct format, but unfortunately the date is wrong. its giving multiple instances of Wed Dec 31 1969 when the real data's year begins at 2004
2

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);

Comments

2

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()
});

2 Comments

hello thanks for your answer, I prefer not to use an external library, but I will if I cant find another solution
I will let you know if i use this library and if it works
1

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);
});

2 Comments

hello where are you getting the variable date in result.date from?
nevermind, i see what you are doing. you are adding a variable date to each object into the dataset. this works great for me. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.