Hey all - I'm trying to enter a date of '05/03/201...
# suitescript
t
Hey all - I'm trying to enter a date of '05/03/2019' in to a NetSuite date field but I keep getting an error that says the date has to be in a 'M/D/YYYY' format. What am I missing? I tried 5/3/2019, 5/3/19, etc to no avail.
m
Are you using SS1 or SS2?
t
SS2
s
have you tried setting the value using a JS Date object?
you can construct it with:
new Date(year, month-1, day)
t
@scottvonduhn this works for me, thanks! Really hate how NetSuite handles their dates eye roll
s
I have complained to them that SS 2.0 took away tools previously available for date manipulation/ If you don't have to manipulate dates, though, you can generally get by with just the N/format module, and using raw Date objects.
t
I was going to mess around with the format module, but just using new Date(dateVariable) works just fine in this scenario thankfully
m
That's good about SS2; SS1 wasn't able to use JS Date objects to set field date values.
t
Yeah, I remember trying to use this same work around in SS1 and it did not play nice
s
okay. I would advise, though, that if you don't know the exact format of the dateVariable, to use a Date parsing library. Formats other than ISO 8601 can be problematic.
t
The date variable should always be in the same format (theoretically) but if I run into any issues with this, I'll try my hand with the format module. Thanks for the advice!
b
In general, be careful about timezones
The timezone used can differ depending on the user
👍 1
s
one extremely common case I run into with dates is getting dates from a search result (which invariably are strings) and needing to use them on a NS record (which are Date objects). I've had to go through the
format
module but this seems like such a common thing I'm wondering if there is a built in feature to support working with dates from a search together with dates on a record?
maybe some utility function I've missed, other than
format.parse/format
?
s
I always use var newdate = new Date(rec.values[fieldname]); to read the date values from a search into a date object. I can then put into any date field in netsuite.
need to careful formatting dates that hit the UI because the user can have a personalized setting for their date formate that is contrary to your preferred format and you can actually get into a loop of your code setting the value in your specified format and the UI changing it to the users preferred format which triggers your code again... and so on...
b
thats actually why the format module is recommended, it knows how to convert dates to string and strings to dates using the current user's timezone and format settings
👍 1
m
having the same issue, trying to set up a date as "today"... any recs..
var today = new Date(); var d = today.getDate(); var m = today.getMonth(); var yyyy = today.getFullYear(); today = m + '/' + d + '/' + yyyy; // change the format depending on the date format preferences set on your account log.debug({ title: 'Test Date Entry', details: 'Value of date is:' + today }); return today = m + '/' + d + '/' + yyyy;
it was working on ss1, i'm trying to convert my small scripts to ss2
b
at the end of the day, its important to remember that a Date is simply a wrapper around a number
you use NetSuite's format module to concert that number into a string because the built in conversions in the Date object will not always work
m
yup.. exactly what i'm reading right now.. it was working with ss1, but nop with ss2
b
your today should be:
Copy code
today =  format.format({value: new Date(), type: format.Type.DATE})
👍 1