When in a 2.1 userevent script I create a new Date...
# ask-stanley-ai
a
When in a 2.1 userevent script I create a new Date() object and set it as the value of a Date/Time field, it resolves to 6 hours earlier than the actual current time. What's the simplest way to fix that? This is running on a server-side script, not clientside. I don't want to have to adjust the code every time daylight saving time comes around. My local time is Central Time Zone, which is less than 6 hours different from Pacific Time Zone, so the fact that NetSuite is in California does not explain the whole 6 hours of offset.
c
AI generated answer (using internal Slack threads):
The issue you're experiencing is due to the fact that JavaScript's
Date()
object uses the timezone of the environment where the code is running. In the case of server-side SuiteScript, this is UTC. To fix this, you can use NetSuite's
format
module to handle timezone conversions. The
format
module provides a
parse
method that can convert a string to a Date object, and a
format
method that can convert a Date object to a string. Both methods accept an options object where you can specify the timezone. Here's an example of how you can use it:
Copy code
javascript
var format = require('N/format');

// Convert a string to a Date object in the current user's timezone
var dateObj = format.parse({
    value: '1/1/2019 12:00 am',
    type: format.Type.DATETIME,
    timezone: format.Timezone.AMERICA_CHICAGO // Central Time
});

// Convert a Date object to a string in the current user's timezone
var dateString = format.format({
    value: dateObj,
    type: format.Type.DATETIME,
    timezone: format.Timezone.AMERICA_CHICAGO // Central Time
});
This way, you don't have to worry about adjusting your code for daylight saving time, as the
format
module will handle it for you. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.869929373
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.869019449
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.867252052
Please provide your feedback on this answer.