I need to find documentation or example code for h...
# suitescript
m
I need to find documentation or example code for how to set a "Time Of Day" field using SS 2.x. I have already tried using the format function and that failed, and I have tried using several different formats to fulfil the goal, with no luck. Any examples, or documentation you guys can point me in the direction of would be awesome.
f
Try something like this using N/format module.
Copy code
nFormat.parse({
  value: new Date(yourDate),
  type: nFormat.Type.DATE,
});
Regards!
a
If you are setting the value to a Date and Time field you need to use nFormat.Type.DATETIME;
Also depending on your initial data type you may need to use format and parse together because one of those methods is overloaded, something like this.
Untitled
m
I haven't tried using Type.DATE or Type.DATETIME yet because I thought the field only took TIMEOFDAY. I'll give that a go, thanks
s
Pretty sure the time of day data type is actually a date time and it displays only the time bit in the UI,
both of these are working for me in the console, where
custitem4
is a time of day field
Copy code
nlapiSetFieldValue('custitem4', '17:00')
nlapiSetFieldValue('custitem4', '6:00PM')
To set in 2.0, you can
rec.setText('custitem4', '17:00')
or
rec.setValue()
with a js Date type in there
m
Yeah that worked for me in the console as well, it was weird that when I get into SS 2.0 and tried using the same strings it choked
Is it common to not have a space between 6:00 and PM from your example?
Would 6:00 PM also work?
also a VERY BIG thank you to those of you who have contributed to helping me resolve this, I really appreciate your time
s
the console doesn't care if there is a space or not, hard to say what is common; I never work with time of day fields, the only reason I have one in my instance is because someone else had the same problem
b
usually the basic answer to these questions is that you use a Date with setValue and a string with setText
s
^
m
@Sandii and @battk - if this were reddit I might give you gold, date worked! Thanks!
So for anyone else running into this, my code now looks something like this: myRecord.setValue({fieldId: 'myField', value: new Date(data.startDate) }); Where data.startDate contains the day and time.
b
beware time zones
m
good point, I may need to ask some questions about how the time is being passed in
b
if you have a choice, get iso-8601 formatted in utc like
2021-01-19T22:22:50.951Z
m
.toISOString() should work off of the date for that right? or do I want a utc specific one?
b
it sounds like you are getting a string as external input
netsuite's Date object (at least in 2.0) is limited
it accepts iso-8601 formatted strings (in the utc timezone)
or you leave your date time string parsing upto the new Date constructor, which basically uses pacific timezone
m
@battk I am now seeing an error saying: "You have entered an Invalid Field Value org.mozilla.javascript.NativeDate@f9ce9b9 for the following field: {myField}" Do I need to use format.format to set the value as a date?
s
I personally always prefer to use setText and use the string version. But in general, yeah format.format should be able to fix your problem.
m
@Sandii can you tell me how I could use set text to set the TimeOfDay? is it as simple as: myRecord.setText({fieldId: 'myField', value: "01:05 AM" }); I'm not sure that's even the correct formatting for that value, but something like that?
s
Yeah that seemed to work in my limited testing
m
okay I can give that a try, and I'll update this thread afterward, thanks
b
i will double down on my warning about date formats
your error could mean that you have an invalid Date, which generally means you passed invalid input to the Date constructor
i personally only bother with ISO-8601 (in utc time) or epoch time
anything else i generally use moment timezone to convert to a Date
s
Same, I almost exclusively use moment to deal with dates unless its something super super simple.