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

Patrick Schult2
Giga Guru

The database always stores dates/times in the UTC time zone so it can be reliably converted into whatever time zone the user is in. Unless you are actually in the GMT/UTC time zone, it's going to be different from a script just because the script doesn't care what your time zone is, it's just telling you what it's stored as in the database.



Check out the GlideDateTime documentation to get a feel for how ServiceNow handles dates and times in scripts. https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_APIRef.dita


Hi Patrick



Thank you for your reply.



However, I still confuse why the Date Picker returns me the different date (i.e. '2017-08-16 02:13:12') while I selected '2017-08-15 19:13:12'.



Capture2.PNG



I understand that GlideDateTime() would return me the UTC timezone which is in 'rightnow' variable in my script, following is the screen shot from my script debugger windows,



Capture.PNG



Thanks in advance.


Some of this depends on how you are accessing "When needed". If you are pulling the value with a client script and sending it to the server with GlideAjax, then you expect it to be sent as whatever time zone the user is in.



If you are accessing "When needed" from the record using a server-side script, then you expect it to be expressed in the UTC time zone.



See these screenshots for an example. In this example, my local time is 2017-08-15 05:40:48, and it appears as such when I pull the value with a client script. If I access the same record from the server, I would get 2017-08-15 12:40:48 because that's the database value in UTC.



So...does that help? It depends on what strDate is coming from, and that can explain why it's different than you expect.



Client.pngServer.png


Hi Patrick



Thank you again for all your replies.



Understand that I will have UTC time when I get the datetime value from database.



My current (as of this post) local time is 16-Aug-2017 9:07 AM. When I pick the date from date picker it is 16-Aug-2017 6:12 PM (pl refer to screen shot 1. However, when I get date picker value in the Business rule script it becomes 17-Aug-2017 01:12 AM (pl refer to screen shot 2 and I don't think any time zone in the world is 17-Aug-2017 currently)




screenshot 1


find_real_file.png


screenshot 2


find_real_file.png


Currently I'm getting this value in Business Rule using GlideAjax using "current.u_when_needed".



Additionally, I become more confused as I refer to following screen shot 4 and 5.



I'm using this Script Include (I supposed it is server side), I pass the strDate which is 2017-08-17 01:25:33 and after it convert and assign to date variable testdate it is still the same date 2017-08-17 01:25:33 but when get date part from datetime variable using "var istoday = testdate.getLocalDate();" it becomes 2017-08-16. is it servicenow issue or the way I handle date is totally wrong?



screenshot 4


find_real_file.png



screenshot 5


find_real_file.png



Thanks in advance.