Not a developer, but I am just trying to understan...
# suitescript
l
Not a developer, but I am just trying to understand how dates are formatted in script in the meantime. I would like to retrieve yesterday’s date (Perth, AU timezone). I have tried this one below, but no luck. Could someone point me to the right direction? Thanks.
var today = new Date();
log.debug('today : ', today);
var yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
log.debug('yesterday : ', yesterday);
var formattedDate = format.format({
value: yesterday,
type: format.Type.DATE,
timezone: format.Timezone.AUSTRALIA_PERTH
});
log.debug('formattedDate : ', formattedDate);
a
I believe the time is pending how you're executing the code. If it is server side you will receive the server time zone. It might be easier for you to just leverage a library that handles the time calculations for you like moment.js
b
netsuite treats dates differently than date and time
specifically, date fields dont actually change depending on timezone, so specifying a timezone for a date does nothing. the date will be formatted in the server's timezone, which will be pacific time
datetimes are the ones where the timezone matter, and its where formatting with N/format will have an effect
l
Thanks. What I did then is to convert yesterday’s date (object) in UTC 0 to AUS using this function below. But the result is still incorrect. It’s 17 hours from UTC 0 instead of just 10. function getYesterdayInAus(yesterday) { const d = new Date(yesterday); const localTime = d.getTime(); const localOffset = d.getTimezoneOffset() * 60000; const utc = localTime + localOffset; const offset = 10; // disregard DST const aus = utc + (3600000 * offset); const yesterdayInAus = new Date(aus); return yesterdayInAus }
looks like it worked when I changed yesterdayInAus to: const yesterdayInAus = new Date(aus).toLocaleString();
Though I still have to convert it to DD/MM/YYYY format
b
still not sure what you are trying to accomplish
Date's on the server are in pacific timezone
yesterday in pacific time is not the same as yesterday in australia time
the offsets you give are kinda wrong
but pacific time is currently utc - 7
Perth (australian western standard time) is utc + 8
thats a 15 hour difference, meaning there are 15 hours of the day where yesterday means a different thing
that actually gets worse due to daylight saving, that 15 hour difference is actually a 16 hour difference half the year since australia doesnt actually observe daylight savings while netsuite will since suitescript follows daylight savings in pacific time
in general, if you dont know these things upfront, you need to be using moment timezone to work in different timezones
with that in mind, lets go over the code, im assuming you kept
Copy code
var yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
which means that the date you are currently working with is 24 hours in the past
Copy code
const d = new Date(yesterday);
const localTime = d.getTime();
const localOffset = d.getTimezoneOffset() * 60000;
const utc = localTime + localOffset;
you add back the offset, which means that you are adding 7 hours to your (eventual) date, meaning you now have a date which is 17 hours in the past
Copy code
const offset = 10; // disregard DST
const aus = utc + (3600000 * offset);
const yesterdayInAus = new Date(aus);
finally, you add another 10 hours to your date, meaning you now have a date which is 7 hours in the past
that Date is using pacific time, so when you try to convert it back to UTC timezone, thats another 7 hour difference, giving you your observed 14 hour difference