I guess it's an easy one. I want the time of the u...
# suitescript
s
I guess it's an easy one. I want the time of the user, not the time of the server. We are in Chicago. When I do : new Date() I get California time. I want Chicago. Any idea ?
s
In JavaScript, all dates are handled internally in UTC. The JS client adjusts it for display according to the configured timezone. Note that the date is not modified, it’s only an output change. In order to output using a different timezone (which still does not affect or modify the underlying date object), you can use a library like
@js-joda/timezone
or
moment-timezone
to control the output.
w
To get the local time of the user, you can create a simple saved search with a result formula field of {now}, then just call the result on that column. I use an Item Saved Search with one Internal ID in the criteria, but it would probably work with any record.
The formula field would be a Date/Time
s
Doing a search as described above will get you the current user time, however it is just a string value, so you won’t be able to use it for any APIs that require a Date object. You can also use N/format (again the output is just a string):
format.format({ value: new Date(), type: format.Type.DATETIME })
✔️ 1
w
True. I only use it currently as a string for PDF documents.
s
If this is just for an Advanced PDF template, you can also just use the special variable
.now
: https://freemarker.apache.org/docs/ref_specvar.html And you can combine that with
?string
to format it however you like: https://freemarker.apache.org/docs/ref_builtins_date.html#ref_builtin_string_for_date