Seems I never get this right ... I have a serverWi...
# suitescript
d
Seems I never get this right ... I have a serverWidget.FieldType.DATETIMETZ field ... I've got the correct Date (using new Date(unix_timestamp) ... I'm using format.format ... and setting the timezone using the
Copy code
runtime.getCurrentUser().getPreference({name: 'TIMEZONE'})
It ALWAYS shows in PST
I know using new Date ... this will produce a PST based Date because all NetSuite servers are set to PST. I simply want that date/time to show in the users timezone.
r
I would consider using a date manipulation/parsing library, such as https://momentjs.com/timezone/. Makes life a hell of a lot easier...use .toDate() on the created moments to get the Date object back before setting the value.
a
moment has been deprecated for about 5 years now, I think luxon replaced it as the goto js date lib? that said i personally don't have an issue with js dates and just use vanilla js
b
what does the code look like
e
moment has been deprecated for about 5 years now, I think luxon replaced it as the goto js date lib?
This is true, but last I checked (some time ago), luxon did not run server-side in NS. None of the recommended replacements for moment did
a
ahh okay, that does explain why ppl in the NS world still recommend it then
b
all the fancy new date libraries use Intl, which suitescript does not have
โ˜๏ธ 1
most of the better date libraries gracefully just dont support the localization features associated with Intl
c
format.format() "If a datetime or datetimetz value is specified, the string in date format is returned in the userโ€™s local app time zone." https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4388843892.html#Related-Support-Article
Based on that, you shouldn't have to get the users datetime preferences and I'm not aware that you can pass a timezone into format() if you want to override the user prefs?
I wouldn't worry about moment being deprecated either. I think it was declared feature complete a while ago and it works.
y
luxon NS server-side I add the code given here for it to work https://stackoverflow.com/a/76486151
d
Love this community for its feedback .. here's the solution I came up with ... in my SuiteQL, I added
(to_date(TO_CHAR(lastmodified, 'MM/DD/YYYY HH24:MI:SS'), 'MM/DD/YYYY HH24:MI:SS') - to_date('19700101', 'YYYYMMDD')) * 24 * 60 * 60 * 1000 as lastmodified_unix
this gave me the milliseconds from Epoch.
format.format({ value: new Date(result.lastmodified_unix), type: format.Type.DATETIMETZ, timezone: format.Timezone.GMT, }) as string,
And now I can update the NetSuite timezone preferences, and it adjusted the output accordingly
a
haha cool solution, that's like the stuff I come up with and then other people are like, no that's crazy don't do that no will understand what you're doing ๐Ÿ˜‚
hopefully there's a comment or 2 in your code somewhere ๐Ÿ˜„
d
comments are for babies
๐Ÿ˜‚ 1
b
are you sure that sql works as expected
to_date('19700101', 'YYYYMMDD')) only actually represents the unix epoch at the gmt timezone
d
Right, so I'm converting the lastmodified value to unix at the gmt timezone as well ... add getting the difference. Exactly what I'm looking for no?
b
i dont have the code, but my guess is that you are calculating the time difference wrong and making up for the mistake by also getting the timezone wrong
beside being weird in general, that strategy relies on the time difference between timezones being constant, which is false on the two dates where daylight savings comes into play
its cleaner to actually use a format model that includes timezone hour and minutes so that you can generate an iso-8601 string with the timezone
d
Well, I'm game to see an alternative. Constraint is simple. Using SuiteQL to get the lastmodified, and present it in the users timezone.