Home > Software design >  Troubleshooting a Date That Won't Parse in Javascript
Troubleshooting a Date That Won't Parse in Javascript

Time:03-08

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.

CodePudding user response:

I'm providing this answer to demonstrate one way to parse out the timestamp in the string you have of the following format, inferred by console.log(r.StartDate); ... /Date(TS)/:

// Provided the date has the following structure in a string

var anyStartDate = "/Date(1657512000000)/";

// Prepare to parse it out by getting the positions of the parentheses

var openParens = anyStartDate.indexOf("(");
var closeParens = anyStartDate.indexOf(")");

// Parse out the timestamp

var timeStampStr = anyStartDate.substring(openParens   1, closeParens); 

console.log( timeStampStr ); // 1657512000000

// Convert timestamp to an int. You can do this when you create the obj, but I am separating it here for explanation purposes.

var timeStampInt = parseInt( timeStampStr );

// Now create a date object

var dateObj = new Date( timeStampInt );

console.log( dateObj );

// (on the machine I'm on):
// Outputs: Mon Jul 11 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
// Or outputs: 2022-07-11T04:00:00.000Z

Now I don't know which library(ies) you are using to handle dates so I just went with the native Date object. You can use this SOLUTION however on further insights to apply it to your code.

The point is once the timestamp is extracted, it can be then used to create a Date object, and thus utilize all the methods that are inherent to that class.

In terms of the "timezone", to get it to UTC, it's already in UTC but javascript formats it to your computer's locale. Internally it's still UTC. There's a way to display it as strictly UTC which is in the docs.

`

  • Related