So, I'm scratching my head on something that shoul...
# suitescript
d
So, I'm scratching my head on something that should be easy. I've got a date/time from the user (using a date/time picker) that will be in the User's timezone preference. And I need to convert that time to GMT. I'd prefer not to include
moment
(since this is the only date/time conversion). Can I use NetSuite's
N/format
module to do this? I keep chasing my tail
m
I run into that sometimes, I think you have to actually make a new date using the difference in timezone, like this
Copy code
const gmtDate = new Date(pacificDate.getTime() + pacificDate.getTimezoneOffset() * 60 * 1000);
b
kinda depends on what format you want your string in
Date.prototype.toUTCString will convert the Date to a string in the gmt timezone
though its usually more common to use Date.prototype.toISOString
there are a bunch of utc related getters if you want to format the string yourself
d
Arg. Why won't my brain understand this. The datestring I'm starting with ISO and in the timezone of the User pref. So, are we saying that if I new Date(userdatestring).toUTCString(), I should be good?
What confuses me is that the ISO String doesn't contain a timezone, so how does the Date object know how many hours to +/-? Or is that magic?
b
Date represent a number (milliseconds since unix epoch)
s
the birth of computers is universal. 1/1/1970
b
that number can be represented by different strings
in different timezones
Date's have an offset representing a timezone (in server suitescript that is basically always Pacific timezone's offset)
and does the math to generate to generate your datetime string
by default, Date's support generating strings for their timezone and gmt
modules like N/format have to do their own math to generate strings for other timezones
same thing in reverse if going from a string to a Date
s
I guess also, there is a section on date/time fields and how NetSuite automatically accepts it as utc, then converts it to user time @darrenhillconsulting Maybe this is what you're looking for? https://system.netsuite.com/app/help/helpcenter.nl?fid=section_1499269203.html
b
toISOString does contain a timezone, with the Z representing Zulu time, which is the same as UTC and GMT