How can i convert this 2023-07-05T00:00:00-06:00 t...
# suitescript
l
How can i convert this 2023-07-05T000000-06:00 to 07/05/2023 in a script?
c
I've been using the momentjs library for dealing with dates. moment('2023-07-05T000000-06:00').format('MM/DD/YYYY')
☝️ 1
b
its a trick question,
2023-07-05T00:00:00-06:00
is the same as
2023-07-04T23:00:00-07:00
and the Dates in serverside suitescript are in pacific time
a
@battk is correct, you can' t just ignore timezones... that said this will do what you want asked for.
Copy code
const dateStr = '2023-07-05T00:00:00-06:00';
const dateArr = dateStr.split('T')[0].split('-');
const myStr = `${dateArr[1]}/${dateArr[2]}/${dateArr[0]}`;
🎯 1
l
how can i define moment library ? is it 'N/moment'
c
It's an external module, download moment.js here, place it in your suitescript folder, and use a relative path:
Copy code
define(['./moment'], function(moment) {
})
a
moment is a 3rd party library https://momentjs.com/ you need to get the file in the file cabinet then reference the filepath in the define
l
got it thanks
this library is amazing
💯 1
b
it used to be the gold standard for javascript date libraries, however its now in maintenance mode
a
didn't the moment devs move to Luxon?
b
however the new alternatives use the internationalization related apis, which suitescript does not expose
so if you have need of handling timezones, moment makes it easier to transition to Moment Timezone
l
Hey guys one more question. why am i getting this error? here is my code trying to set a date field "name":"INVALID_FLD_VALUE","message":"Invalid date value (must be M/D/YYYY)",
Copy code
var ETA = JSON.parse(track_status_response.body).output.completeTrackResults[0].trackResults[0].dateAndTimes[0].dateTime 
   ETA = moment(ETA).format('l')
            
                   log.debug('ETA', ETA)
tried this, but still same issue
Copy code
ETA = moment(ETA).format('M/D/YYYY')
c
Are you getting the error using setValue?
l
yes
c
Try using setText()
l
setText doesnt give me an error but its not setting the field value
b
setValue uses a Date for date fields, setText uses a string for date field
first time users of setText tend to miss out that the parameters of setText arent the same as setValue
general warning, setText is sensitive to different date formats, the code you shared will fail if different users can have different date formats
💯 1
c
The N/format module should help.
l
thanks for your help guys