-1

I have a list of string dates that I retrieve from a website.

const appDate = await page.evaluate(() => {
  const dates = Array.from(document.querySelectorAll('li.det-comment-list-every div.comment-date'))
  return dates.map(font => font.textContent) 
});

I tried to convert them into Date objects in a for loop.

Example of appDate array :

['2018/8/22 13:52', '2018/5/11 22:36', '2018/7/20 07:13', '2018/5/30 04:04', '2018/3/26 18:21', '2019/3/20 17:46', '2019/3/18 13:01', '2019/3/18 07:27', '2019/3/17 23:10', '2019/3/17 20:39' ]

let nDates = [];
for(let date of appDate){
  var d = new Date(date);
  nDates.push(d);
}
console.log(nDates);

However, the console gives me "invalid date".

[ Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date, Invalid Date ]

How can I get my dates to convert into date format?

7
  • Your code is working fine. Commented Mar 21, 2019 at 2:24
  • You are using a for..in to interate through an array..You might have confuse that with for..of Commented Mar 21, 2019 at 2:24
  • I just think so many question before already answered your question, googling "javascript string to date with format" will be the best Commented Mar 21, 2019 at 2:27
  • 2
    I don't want to be that guy, but when it comes to Javascript dates, you should always rely on a third party library. I highly suggest Moment JS which is perfect for this scenario. In this case, you can create a date using moment(date). Commented Mar 21, 2019 at 2:29
  • 1
    @Nicolas - sometimes someone has to be "that guy" :p Commented Mar 21, 2019 at 2:29

2 Answers 2

0

You might have confused that for..of with for..in, vice versa. You have to be careful with iterating arrays using for..in, it iterates through enumerable properties of an object as well. Try doing this instead:

const dates = ['2018/8/22 13:52', '2018/5/11 22:36', '2018/7/20 07:13', '2018/5/30 04:04', '2018/3/26 18:21', '2019/3/20 17:46', '2019/3/18 13:01', '2019/3/18 07:27', '2019/3/17 23:10', '2019/3/17 20:39' ]
        
const nDates = [];
for(let date of dates){
  const d = new Date(date);
  nDates.push(d);
}
console.log(nDates);

If you insist on using a for..in loop, you will need to iterate via its key-value pairs:

const nDates = [];
for(let index in dates){
  const d = new Date(dates[index]);
  nDates.push(d);
}
console.log(nDates);

Or better still, a shorter, one-liner approach:

const nDates = dates.map(date => new Date(date));

console.log(nDates);
Sign up to request clarification or add additional context in comments.

Comments

0

This is a case of wrong use of for..in.. loop. If you try to log your date in for in loop, you'll see that date is actually the index of the element.

var appDate = ['2018/8/22 13:52', '2018/5/11 22:36', '2018/7/20 07:13', '2018/5/30 04:04', '2018/3/26 18:21', '2019/3/20 17:46', '2019/3/18 13:01', '2019/3/18 07:27', '2019/3/17 23:10', '2019/3/17 20:39' ]

let nDates = [];

for(let date in appDate){
   console.log(date);  // 0 1 2 3 4 5 6 7 8 9
   var d = new Date(date);
   nDates.push(d);
}

You can use for..of or any other for loop or map to get the converted dates.

Now that you've updated the question, please verify that the dates array contains valid dates by logging them in the loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.