'Troubleshooting a Date That Won't Parse in Javascript

Getting date properties back from a C# web API that seemed fine but ran into issues when plugging it into DevExtreme DateBox. It was throwing an error of 'getFullYear is not a function' so I checked the dates against this function I found here -

  let r: any = http.post('/get', { Param1: 2, Param2: 1 });
  console.log(r.StartDate);  
  console.log(this.isValidDate(r.StartDate));
  r.StartDate = new Date(r.StartDate);
  r.EndDate = moment(r.EndDate);
  console.log('Start Date', this.isValidDate(r.StartDate));
  console.log('End Date', this.isValidDate(r.EndDate));

    isValidDate(d: any): void {
        if (Object.prototype.toString.call(d) === "[object Date]") {
            console.log('it is a date');
            if (isNaN(d)) { // d.getTime() or d.valueOf() will also work
                console.log('date object is not valid');
            } else {
                console.log('date object is valid');
            }
        } else {
            console.log('not a date object');
        }
    }

  StartDate: "/Date(1657512000000)/"

  not a date object
  undefined

  it is a date
  date object is not valid

  Start Date undefined
  not a date object

  End Date undefined

Not sure why this hasn't come up before with this API but didn't want to look to DevExpress given that I can't produce a valid date.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source