Date Picker Return Wrong Date

aungmon
Tera Contributor

Hi All,

Currently I'm going thru ServiceNow online training and now I'm at "New to ServiceNow" > "Server-side Scripting".

I realized one thing when I'm working on Business Rule and Script Include which is the date picker always give me the wrong date as follows,

Capture1.PNG

Actually I selected today's date as 14-Aug-2017 as follows,

Capture2.PNG

My intention is to compare the selected date and today's date according to the exercise.

Can anyone guide me what is the root cause and what should be the correct way to get selected date?

Thanks in advance.

5 REPLIES 5

Hi Aungmon,



What you are seeing sounds correct if your local timezone is (GMT -7).   If you slow down and evaluate what you are doing at each step, it might become more apparent what's really happening.



  1. You pass in strDate as "2017-08-17 01:25:33"
  2. So essentially you are instantiating testdate=new GlideDateTime('2017-08-17 01:25:33')
    It is important to note here that this object is being created with the value you passed as if it were in UTC.
  3. When you then use ...testdate.getLocalDate();
    You are telling it to adjust the displayed value for testdate to your local timezone, which I'm guessing is (GMT-7)... that is why you are seeing the shift in values.


Be very careful when working with dates, if you are jumping around between the system time and local time while manipulating your variables, it can be easy to corrupt your date/time value.



My first recommendation would be to pick one (i.e., System vs. Local time) and stick with it throughout your calculation.   The values will always be stored in System time, and you will always see them in your local time on UI forms/lists/etc.   But if you absolutely must have the local displayed value of a date/time field, you can use its .getDisplayValue() method in your server script.



My second recommendation would be to give this Wiki a read.   I realize the Wiki itself is going out of fashion with the new docs site around, but I find it to be easier to read.   Once you've skimmed through this article a few times, create a new Fix Script for testing and use it to instantiate some GlideDateTime variables and visualize exactly how these methods affect your values.   I say this because I went through similar headaches (of my own causation), and doing this really helped with understanding of the interactions and effects of sliding between system/local time.




Best of luck,


-Brian