Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

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]);
};

Marty Zigman LinkedIn

Marty Zigman

Holding three official certifications, Marty is widely recognized as a top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. A former Deloitte & Touche CPA and technology executive with CTO roles, he brings over 35 years of leadership in ERP, CRM, and eCommerce business systems. Contact Marty to engage directly.

BiographyYouTubeLinkedInX (Twitter)

5 thoughts on “NetSuite SuiteScript Date Parse

  1. 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.

    Reply
  2. 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!

    Reply

Leave a Reply

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