can you guiys help me? Been strugling with this on...
# suitescript
i
can you guiys help me? Been strugling with this one and have no idea what is happening, for some reason my date is always getting minus 1 day, this is thee code
Copy code
const formatDates = (params, dateKeys) => {
        const fn = 'formatDates';
        const localParams = params;
        log.audit({ title: 'localparams', details: localParams });
        log.audit({ title: 'dateKeys', details: dateKeys });
        try {
            Object.keys(params).forEach((key) => {
                if (dateKeys.includes(key) && params[key]) {
                    log.audit({ title: 'localParams[key] ', details: `${key} : ${localParams[key]}`});
                    const f = format.format({ type: format.Type.DATE, value: new Date(localParams[key]) });
                    log.audit({ title: 'f', details: f });
                    localParams[key] = f;
                }
                
            }) 
        } catch (error) {
            log.error({ title: `Error: ${fn}`, details: error });
        }
        log.audit({ title: 'localParams', details: localParams });
        return params;
}
j
I'm not entirely sure here, but it might have to do with using new Date() within the format() method. It looks like your
custpage_filter_start_date
might already be a proper date object but it's printed as a string in the logs. So you might effectively be passing a date object into a date object into the format method. Just a thought. The other consideration might be something around timezones perhaps?
b
Fri Jun 10 2022 00:00:00 GMT-0500
is passed to the Date constructor, which will construct the Date in Pacific time
which is
Thu Jun 9 2022 22:00:00 GMT-0700
you format it as a Date field, which ignores the time part
thus you get
06/09/2022
only the datetime related types take into account timezone
i
thanks! @Jan Petri and @battk This was the solution,
const dateFor2 = new Date(params[id]);
`const dateString2 = `${dateFor2.getUTCMonth()+1}/${dateFor2.getUTCDate()}/${dateFor2.getUTCFullYear()}`;`
Then send that
dateString
to
format.format({ type: format.Type.DATE, value: new Date(dateString2) });
b
that will be good enough as long as the hours are between 0 and 17, 18 and above risk a difference in date between Central time and utc