I have a RESTlet and struggle with the Time Zone. ...
# suitescript
f
I have a RESTlet and struggle with the Time Zone. It takes the date as PDT which is 7 hours behind. I know the time zone can be set on user level for some script contexts. But I’m not sure as of why I get the time zone that I do. I’ve pushed the time to be correct in SuiteScript, but it’s not a solution that I think is the best. It still logs it as GMT-0700 PDT
trandate: Fri Oct 13 2023 00:00:00 GMT-0700 (PDT)
Copy code
const date = new Date(extractedValue)
const dateValue = format.parse({ value: format.format({ value: new Date(date.getTime() + (7 * 3600000)), type: format.Type.DATE }), type: format.Type.DATE })
log.debug('DATE VALUE', `${key}: ${dateValue}`)
rec.setValue({ fieldId: key, value: dateValue })
b
lets start here
Copy code
const date = new Date(extractedValue)
depending on what version of suitescript you are running and what value is in extractedValue, you will get a Date in Pacific time zone
that date will always be in pacific time zone, the server running your code is running on pacific time and so its Dates are all pacific time
Copy code
new Date(date.getTime() + (7 * 3600000))
which will essentially make a new Date, 7 hours in the future
it will again be in pacific time, for the same reason as above
you then use
Copy code
format.format({ value: new Date(date.getTime() + (7 * 3600000)), type: format.Type.DATE })
which will convert the date to a string in your user's date format, in this case probably something like
2013-10-13
fair chance all the work with messing with the timezone is lost, Date's in netsuite dont care about time or even timezone
all date fields in netsuite ignore time components and are represented by Dates in pacific time
we are finally at
Copy code
const dateValue = format.parse({ value: format.format({ value: new Date(date.getTime() + (7 * 3600000)), type: format.Type.DATE }), type: format.Type.DATE })
although it makes more sense as
Copy code
const dateValue = format.parse({ value: '2013-10-13', type: format.Type.DATE })
which will generate a a date representing
2013-10-13
at midnight in pacific time since netsuite date's dont care about time on timezone
which matches the output you are getting
Fri Oct 13 2023 00:00:00 GMT-0700 (PDT)
is
2013-10-13
at midnight
f
But how can I get it in a different timezone?
b
sorta depends on what the extractedValue is
and what timezone you want to convert it to, and honestly why
f
“date”: “2023-10-13",
b
date fields in netsuite dont care about timezone so you are doing a lot of conversion for a field that doesnt care
if you are starting with “2023-10-13", there is even less of a reason to mess with all the timezone stuff
there is no timezone information in it in the first place
f
I’ve tried to do
const dateValue = format.parse({ value: format.format({ value: new Date(extractedValue), type: format.Type.DATE, timezone: format.Timezone.EUROPE_KIEV }), type: format.Type.DATE, timezone: format.Timezone.EUROPE_KIEV })
But I still get PDT
b
you will not get very far until you learn that your Date will always be pacific time, it will always print in pacific time
also note im using pacific time, which is only PDT for half the year
the other half it will be PST
f
const date = new Date(extractedValue)
log.debug(‘date’, date) = “2023-10-13T000000.000Z” log.debug(‘DATE VALUE’,
${key}: ${date}
) = trandate: Thu Oct 12 2023 170000 GMT-0700 (PDT)
So how do I get it to print in a different timezone?
If that’s possible
b
you can use toUTCString or toISOString if you wanted it to print in gmt time
again, still dont see the point in trying to make it print in a different timezone, there is no timezone information in the first place and setting a date field means you want it in pacific timezone
f
I was just curious
b
start with a input where the timezone actually matters
for example
Copy code
"2023-10-03T13:25:18+03:00"
there is timezone information there to actually use
and then work with a date/time field in netsuite, where the timezone actually matters
f
But the issue is though that the value that gets set on the record is one day behind, which is the problem even without the format.parse
n
I think what you're saying is the date is starting life as midnight in one time zone and being converted to PDT which is moving it back 7 hours to the day before.
f
Yes
extracedValue
is 2023-10-13 const dateValue = format.parse({ value: extractedValue, type: format.Type.DATE }) log says
trandate: 2023-10-13
error: Invalid date value (must be DD.MM.YYYY) Field: trandate
if I wrap extractedValue in new Date() I will get
trandate: Thu Oct 12 2023 17:00:00 GMT-0700 (PDT)
n
So when you get trandate it's coming back with the time data too? Or is it that you're creating a new Date using trandate and it's using your local timezone as GMT and then when it's saved NS is converting it? You should probably stop making a date object out of it to start with if that's the issue since that's causing you grief I suspect.
f
I only recieve the trandate as 2023-10-13, the new Date() adds time and sets it back 7 hours
n
Why are you creating a date object from it, do you need to manipulate it?
f
But if I don’t make it into a Date object i get an error?
Invalid date value (must be DD.MM.YYYY) Field: trandate
n
i think you need to use format.parse
Always stumble on this and have to re-visit it
f
const dateValue = format.parse({ value: extractedValue, type: format.Type.DATE })
This gave me the error
n
That the format was wrong i.e. should be DD.MM.YYYY ?
f
yes
not sure how to handle that
b
format.parse would be an option if the datetime string was in the user's date format
since it isnt, you need to use something else to convert to a Date, in this case, you chose the Date constructor
2023-10-13
will be interpreted as gmt time, so its midnight gmt
so its
2023-10-13T00:00:00+00:00
, which when converted to pacific time, is
2023-10-12T17:00:00+00:00-7:00
, hence the being off by 1 hour
e
I had a similar problem in an integration with Stripe. Receiving a JSON datetime of midnight UTC was getting converted to the previous day for all the reasons mentioned above. Because NetSuite's date fields don't care about time, I just forced the incoming timestamp to be the end of the day before making it a Date:
Copy code
function _asTimestamp(datetime) {
  const endOfDay = datetime.replace('0:00:00', '23:59:59');
  return new Date(`${endOfDay}Z`).getTime();
}
// _asTimestamp('2023-10-03T0:00:00') => 1696377599000
That function doesn't work if all your incoming timestamps are in a different format or aren't all midnight
but the idea of forcing the incoming time later in the day should work
b
normally the solution of adding time like
Copy code
new Date(date.getTime() + (7 * 3600000))
works (although you will want to use a number greater than 7 hours when standard time comes around)
in this case, the strangeness comes from the usage of format and parse
which doesnt really do anything useful
n
I think a better solution is to set the time to midday if you don't care about the time rather than adding a set number of hours. then if the clocks change nobody cares. (appreciate not applicable for all cases depends where in the world you are) get your date object and use .setHours(12,0,0) ; //<--hours/mins/seconds
b
not good enough @NElliott, try it on
Copy code
new Date('2023-10-13')
n
And? Still starts life at midday before NS mangles it and takes off 7 hours?
b
if you are testing on the console, the local timezone needs to be in a negative utc offset
e
Right the
new Date()
part is what's going to take off 7 hours; so the 7 (or 8 or whatever offset) will be removed before you could
setHours()
on it
You need to manipulate the incoming datetime value before turning it to a
Date
Or turn it to a
Date
and adjust it by adding or subtracting the offset hours as appropriate, but just setting the hours will not work
n
I see now my mistake. In reality I mentioned it because an external system was sending me a date that was 000000 and I asked them to set it to 120000 since NS altered it when I tried to work with it. Slightly different scenario, my bad. 😑