User Date Format and .setValue (date / time) inconsistencies

CarlStoepker
Giga Contributor

This script takes start_date from a record, adds 14 days and puts the result into end_date of the same record:

    var inpDate = new GlideDateTime();

    inpDate.setDisplayValue(chg.start_date);

    inpDate.addDays (14);

    chg.end_date = inpDate;

    chg.update();

The funny thing is that I get the following in the form:

Start Date (start_date):                       2014-02-18 17:00:29                                          

End Date (end_date):                                       2014-03-04 22:00:29

The difference between the two is 14 days and 5 hours!!!!

Where is the 5 hours coming from and how can I make this work correctly?

Het bericht is bewerkt door: Carl Stoepker Updated the title to better reflect the content

5 REPLIES 5

eican
Kilo Guru

The GlideDateTime object is very picky with how you get the value and also how you set it.


Using the one or the other (display value yValue(asDisplayed) or system value yValueInternal(value)) has a difference on which timezone is used when setting the value.



If you read is as a user you will get the display value but if you write it as system you will get a timezone difference.


Best is to script this step-by-step and checking always the value which is returned.


Especially when you write it to the form, check the value which was calculated by the script and the one which was set in the field.



You probably found it already but here is the link to the wiki: https://wiki.servicenow.com/index.php?title=GlideDateTime


Something like this?


    var inpDate = new GlideDateTime();


    inpDate.setValue(chg.start_date);                 // do not use .setDisplayValue in this case !!!


    inpDate.addDays (14);


    chg.end_date = inpDate;


    chg.update();



Thx you are my hero!!!


Correct



Just wondering why you are using different code to set the date and time in the two fields.


For the start_date you are using "setValue" while for the end_date you simply assign it.



I would use either or but not mix the way to assign the value. That way you also make sure that the value is of the same format.


I have done some more digging on this topic with the following findings:



6 serious findings:


  • 'dd.MM.yyyy' user date format is not supported by setValue (and setDateValue?), although available in the user date format choice list.
  • '.setDateValue' function, for every user date format, returns an incorrect date value (might be by design).
  • '.setValue' function with 'dd-MM-yyyy' and 'dd/MM/yyyy' user date format swaps MM and dd when dd<=12.
  • gs.dateDiff function with 'yyyy-MM-dd' user date format doesn't calculate correctly.
  • '.setValue' function with 'dd/MM/yyyy' user date format doesn't return the hh:mm:ss part of the date/time in case dd>12 (this has been tested separately).
  • '.getDisplayValue()' function with 'MM-dd-yyyy' and 'yyyy-MM-dd' user date formats doesn't seem to consistently calculate the local time hours.


Conclusion:


The only user date format that supports '.setValue' and 'gs.dateDiff' functions properly is:


MM-dd-yyyy



I used the following code to proof this:


var inpDate = new GlideDateTime();


var dateformat = Array ('MM-dd-yyyy',                 'dd/MM/yyyy',                 'dd-MM-yyyy',                 'yyyy-MM-dd',                 'dd.MM.yyyy');


var passDate = Array     ('04-06-2014 09:01:21','06/04/2014 09:01:21','06-04-2014 09:01:21','2014-04-06 09:01:21','06.04.2014 09:01:21');


var NowDT = gs.nowDateTime();           // In Display Value!!


var trailer = String ('+++');           // For easy log file reading



var me = new GlideRecord('sys_user');


if (me.get(gs.getUserID())) {


    for (var i = 0; i < dateformat.length; i++)   {


          me.date_format = dateformat[i];


          me.update();



          // Date conversion tests


          inpDate.setValue(passDate[i]);


          gs.log (trailer + " inpDate.setValue with " + dateformat[i] +


                        ":           inpDate:                     " + inpDate +


                        " - inpDate.getDisplayValue(): " + inpDate.getDisplayValue() +


                        " - inpDate.toString(): " + inpDate.toString());



    inpDate.addDays (-14);


          gs.log (trailer + " inpDate.setValue with " + dateformat[i] +


                        ":           inpDate-14:               " + inpDate +


                        " - inpDate.getDisplayValue(): " + inpDate.getDisplayValue() +


                        " - inpDate.toString(): " + inpDate.toString());


          gs.log (trailer + " inpDate.setValue with " + dateformat[i] +


                                              ":           NowDT: " + NowDT +


                        ": gs.dateDiff(true): " + gs.dateDiff(NowDT, inpDate.getDisplayValue(), true) +


                        " gs.dateDiff(false): " + gs.dateDiff(NowDT, inpDate.getDisplayValue(), false));



          inpDate.setDateValue(passDate[i].getValue());


          gs.log (trailer + " inpDate.setDateValue with " + dateformat[i] +


                        ":   inpDate:                     " + inpDate +


                        " - inpDate.getDisplayValue(): " + inpDate.getDisplayValue() +


                        " - inpDate.toString(): " + inpDate.toString());



    inpDate.addDays (-14);


          inpDate.setDateValue(passDate[i].getValue());


          gs.log (trailer + " inpDate.setDateValue with " + dateformat[i] +


                        ":   inpDate-14:               " + inpDate +


                        " - inpDate.getDisplayValue(): " + inpDate.getDisplayValue() +


                        " - inpDate.toString(): " + inpDate.toString());


          gs.log (trailer + " inpDate.setDateValue with " + dateformat[i] +


                                              ":           NowDT: " + NowDT +


                                              ": gs.dateDiff(true): " + gs.dateDiff(NowDT, inpDate.getDisplayValue(), true) +


                        " gs.dateDiff(false): " + gs.dateDiff(NowDT, inpDate.getDisplayValue(), false));


    }


}


else {


    gs.log (trailer + 'I do not exist!');


}




Output (lines have been reshuffled):


+++ inpDate.setValue                 with dd-MM-yyyy: inpDate:             2014-06-04 09:01:21 - inpDate.getDisplayValue(): 04-06-2014 11:01:21 - inpDate.toString(): 2014-06-04 09:01:21


+++ inpDate.setValue                 with dd-MM-yyyy: inpDate-14: 2014-05-21 09:01:21 - inpDate.getDisplayValue(): 21-05-2014 11:01:21 - inpDate.toString(): 2014-05-21 09:01:21


+++ inpDate.setValue                 with dd-MM-yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 11966472 gs.dateDiff(false): 138 12:01:12



+++ inpDate.setDateValue with dd-MM-yyyy: inpDate:             2014-05-21 09:01:21 - inpDate.getDisplayValue(): 21-05-2014 11:01:21 - inpDate.toString(): 2014-05-21 09:01:21


+++ inpDate.setDateValue with dd-MM-yyyy: inpDate-14: 2014-05-07 09:01:21 - inpDate.getDisplayValue(): 07-05-2014 11:01:21 - inpDate.toString(): 2014-05-07 09:01:21


+++ inpDate.setDateValue with dd-MM-yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 10756872 gs.dateDiff(false): 124 12:01:12



+++ inpDate.setValue                 with dd/MM/yyyy: inpDate:             2014-06-04 09:01:21 - inpDate.getDisplayValue(): 04/06/2014 11:01:21 - inpDate.toString(): 2014-06-04 09:01:21


+++ inpDate.setValue                 with dd/MM/yyyy: inpDate-14: 2014-05-21 09:01:21 - inpDate.getDisplayValue(): 21/05/2014 11:01:21 - inpDate.toString(): 2014-05-21 09:01:21


+++ inpDate.setValue                 with dd/MM/yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 9374472 gs.dateDiff(false): 108 12:01:12



+++ inpDate.setDateValue with dd/MM/yyyy: inpDate:             2014-05-21 09:01:21 - inpDate.getDisplayValue(): 21/05/2014 11:01:21 - inpDate.toString(): 2014-05-21 09:01:21


+++ inpDate.setDateValue with dd/MM/yyyy: inpDate-14: 2014-05-07 09:01:21 - inpDate.getDisplayValue(): 07/05/2014 11:01:21 - inpDate.toString(): 2014-05-07 09:01:21


+++ inpDate.setDateValue with dd/MM/yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 8164872 gs.dateDiff(false): 94 12:01:12



+++ inpDate.setValue                 with MM-dd-yyyy: inpDate:             2014-04-06 09:01:21 - inpDate.getDisplayValue(): 04-06-2014 11:01:21 - inpDate.toString(): 2014-04-06 09:01:21


+++ inpDate.setValue                 with MM-dd-yyyy: inpDate-14: 2014-03-23 09:01:21 - inpDate.getDisplayValue(): 03-23-2014 10:01:21 - inpDate.toString(): 2014-03-23 09:01:21


+++ inpDate.setValue                 with MM-dd-yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 4276872 gs.dateDiff(false): 49 12:01:12



+++ inpDate.setDateValue with MM-dd-yyyy: inpDate:             2014-03-23 09:01:21 - inpDate.getDisplayValue(): 03-23-2014 10:01:21 - inpDate.toString(): 2014-03-23 09:01:21


+++ inpDate.setDateValue with MM-dd-yyyy: inpDate-14: 2014-03-09 10:01:21 - inpDate.getDisplayValue(): 03-09-2014 11:01:21 - inpDate.toString(): 2014-03-09 10:01:21


+++ inpDate.setDateValue with MM-dd-yyyy: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 3070872 gs.dateDiff(false): 35 13:01:12



+++ inpDate.setValue                 with yyyy-MM-dd: inpDate:             2014-04-06 09:01:21 - inpDate.getDisplayValue(): 2014-04-06 11:01:21 - inpDate.toString(): 2014-04-06 09:01:21


+++ inpDate.setValue                 with yyyy-MM-dd: inpDate-14: 2014-03-23 09:01:21 - inpDate.getDisplayValue(): 2014-03-23 10:01:21 - inpDate.toString(): 2014-03-23 09:01:21


+++ inpDate.setValue                 with yyyy-MM-dd: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 63325879281 gs.dateDiff(false): 732938 10:01:21




+++ inpDate.setDateValue with yyyy-MM-dd: inpDate:             2014-03-23 09:01:21 - inpDate.getDisplayValue(): 2014-03-23 10:01:21 - inpDate.toString(): 2014-03-23 09:01:21


+++ inpDate.setDateValue with yyyy-MM-dd: inpDate-14: 2014-03-09 10:01:21 - inpDate.getDisplayValue(): 2014-03-09 11:01:21 - inpDate.toString(): 2014-03-09 10:01:21


+++ inpDate.setDateValue with yyyy-MM-dd: NowDT:               02-01-2014 22:00:09: gs.dateDiff(true): 63324673281 gs.dateDiff(false): 732924 11:01:21



+++ inpDate.setValue with dd.MM.yyyy: inpDate: - inpDate.getDisplayValue(): - inpDate.toString():



Grtz



Carl