-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?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
glhe13
  • 469
  • 1
  • 4
  • 10
  • Your code is working fine. – Mohammad Usman Mar 21 '19 at 02:24
  • You are using a for..in to interate through an array..You might have confuse that with for..of – wentjun Mar 21 '19 at 02:24
  • I just think so many question before already answered your question, googling "javascript string to date with format" will be the best – dungmidside Mar 21 '19 at 02: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](https://momentjs.com/) which is perfect for this scenario. In this case, you can create a date using `moment(date)`. – Nicolas Mar 21 '19 at 02:29
  • 1
    @Nicolas - sometimes someone has to be "that guy" :p – Jaromanda X Mar 21 '19 at 02:29
  • @MohammadUsman It still doesn't work on my console. I am retrieving the dates from a website. Does that affect the conversion? – glhe13 Mar 21 '19 at 02:35
  • @glhe13 - his comment is completely wrong - in Chrome it looks like it works, but if you read the dates, they are like 1 jan 2000, 1 jan 2000, 1 feb 2000 etc (yes, the first two are the same) – Jaromanda X Mar 21 '19 at 02:38

2 Answers2

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);
wentjun
  • 40,384
  • 10
  • 95
  • 107
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.

Richard
  • 439
  • 1
  • 6
  • 25
DEVCNN
  • 614
  • 4
  • 13