How can I change this date format: Fri Oct 07 2022...
# suitescript
p
How can I change this date format: Fri Oct 07 2022 000000 GMT-0700 (PDT) to 10/07/2022 so I can use in a custom field on a sales order? I tried using format.format as below but that did not work:
Copy code
var pudate = format.format({
        value: pickupdate,
        type: format.Type.DATE
    });
Figured it out using javascript and new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); I am sure there is a netsuite way but not sure what that is.
s
using native javascript IS a valid 'netsuite way' since NS supports JS standards. I generally only use netsuite apis around date formatting when we need to format according to user preferences in NS
b
general rule of date fields is to use a Date with setValue and a string with setText
importantly requires understanding the difference between a string and a Date
p
I have also had luck using the format.parse method from the N/format module too.
var pudate = format.parse({value: pickupdate, type: format.Type.DATE});
b
its best to keep in mind how types work when working with N/format
format.format will take a Date as an input and output a string as an output, though will also output the input Date if the formatting fails
format.parse will take a string as an input and output a Date as an output, though will also output the input string if parsing fails
the reason you deal with N/format is that it automatically converts to the date format of the current user, which can be different per user
p
Thanks for the explanation!