Maybe an odd question but in SS2.0 can you set a d...
# suitescript
n
Maybe an odd question but in SS2.0 can you set a date field using setText? I thought so but computer says no...
m
You can but I think it needs to be in a pretty precise format matching the user's preferences - I usually format date strings to a Date object and use
setValue
for that exact reason.
n
Yeah I experienced something odd last night. Script ran at 1:00am local time. I can see my "new Date()" reflects the correct local time. I set a date field using that JS Date obj on a custom record and the date was yesterday! system notes show the record was created on my local time. It appears NS decides to calculate the time taking in to account the server time offset even though i never asked it to which pushes the date back a day because of it's proximity to midnight. If I put it through N/format and spit it out as a date it sets the time to 7am. That works for me if, when setting the Date field the date matches "today" or if it decides to mangle it and adjust the server time diff if it's still today. I'll have to see what happens at 1am tomorrow!
m
I've had issues with server time offset before as well. Thankfully we're just 2 hours ahead of our server so I have overnight scripts run at 3am to be sure the offset doesn't screw anything up, but I could see that getting a lot trickier if you're further offset than that. I've also just hard-coded in a 2 hour adjustment to
new Date()
calls in some instances, like so:
Copy code
var serverTime = new Date();
var localTime = new Date(serverTime .getTime() + (120 * 60000));
n
Interestingly I've ouput the date obj to execution log and then manually grabbed the hours/mins/seconds and despite the object showing as 7am when I use n/format, the actual h/m/s are 0.0.0 so it's doing some stupid conversion when it outputs the object to the log. NS 🎖️👿
b
keep in mind that by default dates are in the server's timezone
this is not required to match the current user's timezone
timezone logic that doesnt match pacific time tends to suck
n
Yeah I even fell back on the "current time" custom record to get the date/time at one point it's largely irrelevant, NS seems to mangle dates and to make matters worse you cannot trust outputting the JS date object to the execution log, it lies. 😮
m
Do all servers run on pacific time regardless of location? I'd feel a lot better about my hard-coded solution if so, especially since it's become a lot harder to know what server you're on now that it's not part of the url anymore.
n
might be better doing serverTime.getTimezoneOffset() which gives you the minutes offset, in my case 7 hours / 420 mins
b
i believe its always pacific time from the accounts i see
not documented anywhere that i know of, so can change whenever
n
I'd be happy if when I do new Date() and put that date obj in to a date field that it didn't try and be smart about it. The log showed the date object correctly as 11500 am in reality it had dropped off several hours which put my date field a day earlier! Oh well, if it were easy NS devs wouldn't be paid as well...
b
what are you using to log dates?
m
Yeah, it's the undocumented part that makes me a bit uneasy. I guess using
getTimezoneOffset()
and calculating from your local offset might be the best option, but even that assumes that DST changes on the same dates for both locations.
n
Copy code
var today = new Date();
log.audit({title: 'today', details: today});
But if you log out today.getHours() etc it's completely different hours
b
getHours gives you the hours in Pacific timezone
your log gives it to you in GMT time
n
Yeah but logging the date object suggests it's in my local l time
b
Copy code
var today = new Date();
log.audit({title: 'today', details: today.toString()});
n
So it's confusing because I somewhat "assumed" if the log tells me my date is something then that's what it is not translating it or am I incorrect for using getHours()?
I've been coding on this platform for about 7 years and still find it a minefield to work with!
oh well, I'm done for the day, been fun.
(thankfully I clock off based on my local time 😄 )
😁 1