Anyone have any idea why when using `format.parse`...
# suitescript
k
Anyone have any idea why when using
format.parse
for a date it only returns an object for some users and not for others?
b
if it outputs the same value as the input value, then its not in the required format
k
OK. So if it not working for myself and one other and it’s coming from an input field what would be causing the issue. The value is being passed from a param in the suitelet. Within the URL Params.
b
code?
k
Copy code
function getdatedifference(startdatefldval, enddatefldval) {
    try {
        log.debug('startdatefldval  ', startdatefldval);
        log.debug('startdatefldval typeof ', typeof startdatefldval);
        log.debug('enddatefldval  ', enddatefldval);
        log.debug('enddatefldval typeof ', typeof enddatefldval);
        var date1 = format.parse({value: startdatefldval, type: format.Type.DATE});
        log.debug('date1', date1);
        log.debug('date1 string', JSON.stringify(date1));
        log.debug('date1 typeof', typeof date1);
        var date2 = format.parse({value: enddatefldval, type: format.Type.DATE});
        log.debug('date2', date2);
        log.debug('date2 string', JSON.stringify(date2));
        log.debug('date2 typeof', typeof date2);
        var date1mill = date1.getTime();
        //log.debug('date1 mill', date1mill);
        var date2mill = date2.getTime();
        //log.debug('date2 mill', date2mill);
        // To calculate the time difference of two dates
        var Difference_In_Time = date2.getTime() - date1.getTime();
        //log.debug('Difference_In_Time', Difference_In_Time);
        // To calculate the no. of days between two dates
        var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
        //log.debug('Difference_in_Days', Difference_In_Days); // 86400000 milliseconds in one day

        //ADDED TO ALLOW FOR SAME DAY PROJECT CREATION.
        if(Difference_In_Days == 0 ){
            Difference_In_Days = 1;
        }else{
            Difference_In_Days;
        }
        //To display the final no. of days (result)
        if(hasValue(Difference_In_Days)){
            //log.debug("Total number of days between dates :" + Difference_In_Days);
            return Difference_In_Days;
        }else{
            return false;
        }
That is the code for the function where it is failing
b
what are the values for startdatefldval and enddatefldval?
k
datevalue 18-Feb-2020
setdateFormatvalue 18 February, 2020
message has been deleted
b
whats the value of runtime.getCurrentUser().getPreference({ name: "DATEFORMAT" });
from the script that is using format.parse
k
just grabbing it now.
DD/MM/YYYY is what gets logged.
Do you reckon i would need to use format.format before parse?
b
your input does not match formats
where is your input coming from
k
So user prefs are setup differently then.
Input is coming from a HTML5 date input on suitelet form
b
dont use N/format if you arent using suitescript
N/format is for things from netsuite
k
use new Date() then.
b
netsuite has its own string formats independent from a browser
k
Just changed my user prefs to the company prefs and working now.
b
honestly your best chance is to convert your html date value into a Date client side, and then post it to your suitelet using its unix timestamp or ISO8601 string
k
hmmmm ah well. going to have to work it out so that if prefs are changed it willl still work.
a
You can spend a lot of time trying to figure out date problems and eventually fix them or you can just use moment and save a lot of time.
k
Fixed it. Used new Date() lol
Works for me this time.