have you verified that you are actually getting th...
# suitescript
s
have you verified that you are actually getting the expected JS Date object back from format.parse?
c
No
s
also, you will need to handle the case where month = 11, and you have to advance the year instead, and set month to 00, if using pure JS
c
Copy code
log.audit('tranDateObject', typeof tranDateObject);
result was 'Object'
so yeah, looks like a date object
I think it's just a formatting issue as the end result looks like: 1625727600000
So I tried this
s
try calling
tranDateObject.toUTCString()
JS Date’s don’t give many formatting options, unfortunately. But most SS 2.x api calls that accept a date can take a raw JS Date object, so it shouldn’t matter too much (unless you need to display the formatted value somewhere)
c
Happy to pass a date object
format.format appears to be returning a number though
Rather than a date
s
This is why I prefer to use a Date library. Things start to get very messy quickly. And most date libraries give you absolute control over how to parse and print Dates as strings, so it’s definitely worth it. There is negligible overhead.
c
Yeah, probably the right answer
b
i would recommend using format.parse to change the string into a Date
✔️ 1
and use the Date with the Date library
if you use the string with the Date library, make sure you properly account for user date format (or make sure its not applicable)
c
It will be applicable as the account has users all over the world
I think my pure SS2.1 approach was working. I just need to convert the UNIX timestamp back to a date
b
wait a minute
are you literally using
Copy code
let newMonth = tranDateObject.setMonth((tranDateObject.getMonth() + 1));
c
yes
and similar for adding days too
b
setMonth is a setter
its not modern
s
ah, right. it mutates the Date it is called on, it does not return a new Date. I personally dislike such functionality. Be aware that Moment.js objects are also mutable, though, unlike some other libraries which have immutable date-like objects
c
I'll probably add moment.js and use that.
s
Yeah theres really no reason to reinvent the wheel, dates are a pain in the ass, just use a date library.
s
FWIW, Chrome Dev Tools recommends Day.js as a lighter alternative to Moment.js. I haven’t used it yet, but if I was starting fresh, I’d probably look into it. Moment is officially deprecated, and they actually recommend using another library instead of theirs for any new work.
Though it may depend upon
Intl
being available, which still isn’t the case for any version of SuiteScript. I know Luxon requires Intl
c
I just added moment haha
It's deprecated but it doesn't really matter. A lighter alternative assumes you're loading it into the browser where for this it's serverside
If I'm taking the trandate from an order, I'm parsing it as DD/MM/YYYY but the system preferences may have a different format.
What's the best way to handle that?
Untitled.js
ideally I wouldn't care what the date format is but trandate is a localised string on the salesorder
b
my vote remains use N/format to turn it from a string into a Date
and then use the Date with moment
if you want pure moment, you need to use N/runtime to get the date preference
👆 1
if you are working with a record, then trandate should already be a Date
c
I thought the result of getValue('trandate') would be a string
s
In a search yes, not necessarily with record.getValue(), it notes that in the help docs
true news 1