Having Trouble setting a date field I keep getting...
# suitescript
m
Having Trouble setting a date field I keep getting the error Invalid date value (must be M/D/YYYY) using this code I thought I would fix this var rawdate = new Date(newdata[0].values.trandate); var newtrandate = format.format ({ value : rawdate, type: format.Type.DATE }); What am I missing here
b
what value is in the rawdate
in general, you use the format module to parse the date string into a Date
and then use the Date itself to set the field
m
its a date string
from my reduce array
b
as in which Date did new Date interpret the string as
you can log its value using rawdate,getTime()
or rawdate.toISOString()
m
If you have the option, I would recommend dropping a copy of moment.js into your SuiteScripts file cabinet, then using that for date/datetime manipulation. I've found it well worth the quick hassle of dropping the library in.
m
thanks I will try that
btw here is what I get for the raw date and then formatted date log raw date Debug "2018-06-26T070000.000Z" newtrandate 6/26/2018
m
A snippet from my UE script which uses moment.. Note that moment.js lives in the FileCabinet under "/SuiteScripts/Libraries/moment.js" define(['N/record', 'N/search', 'N/url', 'N/ui/serverWidget', '/SuiteScripts/Libraries/moment'], function(record, search, url, serverWidget, moment) {
b
that looks like it interpreted the date correctly
how are you setting the Date field
m
cco.setCurrentSublistValue({ sublistId: 'line', fieldId: 'custcol_co_date_sold', value: newtrandate });
b
setValue for Date fields uses a Date
setText for Date fields uses a string
i recommend using setValue with a Date