This article is relevant if you are working with NetSuite SuiteScript and are getting “Invalid Date” and other date issues.
Background
Joe Son, fellow blogger and SuiteScript Developer, wrote an article about not using JavaScript’s native Date() function in favor of NetSuite nlapiStringToDate() function. However, I have found that sometimes NetSuite is not so forgiving about Date strings relative to the Browser. Consider the following:
//this will fail in NetSuite, but work perfectly in Chrome debugger y = new Date("2014-05-14 18:29:58") //this too will fail, likely because the time element is not clean y = nlapiStringToDate("2014-05-14 18:29:58", 'datetimetz')
After a fair amount of frustration, I realized that I just need to produce my own Date string parsing function.
Date String Parsing Algorithm
See the following parsing function. Modify it to fit your Date String challenge.
//NetSuite does not understand our datetime string so parse and reassemble; it returns good date function parseDateString(ds){ var a = ds.split(' '); // break date from time var d = a[0].split('-'); // break date into year, month day var t = a[1].split(':'); // break time into hours, minutes, seconds return new Date(d[0], d[1]-1, d[2], t[0], t[1], t[2]); };
This solved an issue for me, thanks! Trying to set a date field from an ISO date string when NS was looking for a localized date I think.
Here in 2021 this is still super helpful because netsuite refuses to handle dates in a sane manner. You saved me hours of frustration. Thank you!
Kevin,
I am glad to hear this. Dates are tricky in NetSuite, even in SuiteScript 2.x versions.
Best.
Marty