NetSuite SuiteScript Date Parse

NetSuite Technical

Tags: , , ,

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]);
};
Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - YouTube

About Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

Biography • Website • X (Twitter) • Facebook • LinkedIn • YouTube

5 thoughts on “NetSuite SuiteScript Date Parse

  1. Orion says:

    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.

  2. Kevin says:

    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!

  3. Marty Zigman says:

    Kevin,

    I am glad to hear this. Dates are tricky in NetSuite, even in SuiteScript 2.x versions.

    Best.

    Marty

Leave a Reply

Your email address will not be published. Required fields are marked *