-2

I am trying to convert a date string of the format dd.MM.yyyy to a Date object. I have tried the following :

if( typeof myVar === 'string') {
      let myDate = new Date(myVar);
      if (myDate.getTime() < this.getMinDate().getTime()) /** compare two dates */
          //omitted source code 

}

this.getMinDate().getTime() reads 1612989937059 while myDate.getTime() reads NaN. I have tried other date format patterns such as dd-MM-yyyy and dd/MM/yyyy. None of them seems to work either.

  • https://en.wikipedia.org/wiki/ISO_8601 – Jared Smith Feb 09 '21 at 20:53
  • `new Date(...)` requires the input to be in [a specific format](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#syntax). –  Feb 09 '21 at 20:54

1 Answers1

0

If you absolutely have to have it in that format, you can rearrange it quick before using the date object as it expects a certain format..

Something like this could work.

if( typeof myVar === 'string') {
      let dateArr = myVar.split('.');

      let myDate = new Date(dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2]);

      if (myDate.getTime() < this.getMinDate().getTime()) /** compare two dates */
          //omitted source code 

}
Benito
  • 52
  • 8