I am using a suitelet to create a form which in tu...
# suitescript
v
I am using a suitelet to create a form which in turn creates a custom record The suitelet has the following code to retrieve the current date in the NZ timezone
Copy code
var currentDate = new Date();
    var formattedDate = format.format({
      value: currentDate,
      type: format.Type.DATE,
      timezone:format.Timezone.PACIFIC_AUCKLAND
    });
    log.debug("formattedDate", formattedDate);
When I use the resulting value to set a date field on the custom record, I receive the error per the screenshot. I assume this is because the formattedDate variable is a string? I have tried to use this to convert the string to a date format.
Copy code
var parsedDate= format.parse({

value:formattedDate,

type: format.Type.DATE

});

log.debug("parsedDate",parsedDate)
However, this returns a date/time object in a different format Can anyone see where I am going wrong with this?
b
date fields dont actually care about timezones
they are always shown in relation to pacific timezone
otherwise, for date fields, you use a Date with setValue, and a string with setText
v
The field on the custom record is a date field
Would i be able to use set text in that case? The suitelet sets the field correctly so long as the date is not manipulated (i.e still in PST)
b
your manipulations are probably misguided unless you are adding or subtracting days
if you are just changing the day, then you should be able to use the date with setValue
if you wanted to use setText, you would need to format it into a string first
this will go very badly if you dont know the difference between a string and a Date
v
The field in the suitelet is auto-populating with the "current date" which is in PST when I use var currentDate = new Date(); I need this to populate with the current date in another time zone (PACIFIC_AUCKLAND) On submit, the suitelet is supposed to set the field on a custom record The field it is setting is a date field type The manipulation is necessary to get the date in the right time zone however, this is returned as a string and the field that needs to be set is a date field (i.e. value field) I cannot use format.parse as this returns a date time format instead of just date So my question is: how do I convert the string variable (formattedDate) to a date value so I can use this to set the value on the custom record per the snippet below:
sampleRequest.setValue({
fieldId: "custrecord_request_date",
value: formattedDate,
});
Does that make sense?
b
formattedDate is a string, you cannot use it with setValue
your attempt to use format.format doesnt do what you think it does, it doesnt take into account timezones, since Date fields dont
it will just fomat the date into a date sting using the timzone of the server, which will be pacific time
v
ok I was going by this sample on the Suitescript help on the NetSuite site: It seemed to imply different date strings are returned depending on what is specified in the timezone So for my use case, do I need to use a third party library to get the current date of the logged in user?
b
datetime fields are the ones that care about different timezones, date's dont
you probably want to be extra clear on what you want
the timezone of the user, the timzone that the user set in their preferences, the timezone of the server, the timzone set in the server's preferences are all different things
a lot of the time, the differences dont matter, date fields arent really precise enough for that purpose
v
I want the time zone of the user (per their preferences) or the NetSuite account's time zone (per company information page) These are the same so it doesn't matter The user/account is a day ahead of PST which is why I want the date on the suitelet to reflect the date per the user preferences/company preferences and not the default PST date returned
b
the Date objects you work are not guaranteed to be in either of those timezones
if you need to set time level information on those dates, then my recommendation is moment timezone
if you just want something like the Date now, then using N/format to format the Date as a datetime string is viable since it allows you to format it to a specific timezone
in which case the output is a string, and you should use setText instead of setValue
v
In case this helps anyone else with the same problem, the following works and resolved the issue:
var currentDate = new Date();
var formattedDate = format.format({
value: currentDate,
type: format.Type.DATETIME,
timezone:format.Timezone.AUSTRALIA_SYDNEY
});
log.debug("formattedDate", formattedDate);
var parsedDateStringAsRawDateObject = format.parse({
value: date,
type: format.Type.DATE
});
the format.format function with enum value under timezone returns the correct date in the specified timezone Where I was going wrong was the
formattedDate
variable: Under type, I had: type: format.Type.DATE this needed to be type: format.Type.DATETIME, The custom field , being set by the Suitelet, is type 'Date'. However, the conversion to date occurs with
parsedDateStringAsRawDateObject
which sets the field value to the required date format
t
Hey @Vernita, just letting you know this helped me solve a datetime issue I was stuck on for a while so thanks heaps! 🙌