The CreatorCon Call for Content is officially open! Get started here.

Current date

uma17
Tera Guru

Hi,

I am having a scheduled job which runs every hour to check if any of the change task have start date & time <= current date & time.

I have used

    var today = new GlideDateTime();

    var curDate = today.getDate().getNumericValue();

But every time I run the job the curDate = 1433721600000 it is not changing.

Thanks

Uma

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

The function getDate() returns only date but not time and hence you will get same value for every hour in a day. Try following




    var today = new GlideDateTime();


    var curDate = today.getNumericValue();



View solution in original post

4 REPLIES 4

joshua_bice
Giga Expert

How many different days have you checked? Your "today" variable will store the time and date of when it was initialized, and does so in GMT. In other words, your getDate() method will always show the same date in GMT until the next day, if you re-initialize the variable via script, in which it will go up by the amount of milliseconds in 24 hours.



Also, I generally recommend getLocalDate() and getLocalTime() for the sake of getting the value you're expecting rather than GMT.


Tanaji Patil
Tera Guru

Hi Uma,



The easiest way will be to use GlideRecord on 'change_task' table and add an encoded query.


You just need to add the filters on the required table in the list view and copy that query and use it in the code.


For eg. 'active=true^sys_created_onRELATIVEGE@hour@ago@1' will get all active records created on or after 1 hour i.e. within last 1 hour. This way you don't need deal with the date and time functions and time zones.


For using encoded query in GlideRecord see-


Encoded Query Strings - ServiceNow Wiki


http://wiki.servicenow.com/index.php?title=GlideRecord#addEncodedQuery&gsc.tab=0


» GlideRecord Query Cheat Sheet



You can also use 'gs.dateDiff()'.


Check GlideSystem Date and Time Functions - ServiceNow Wiki for info and example.



Thanks,


Tanaji


Gurpreet07
Mega Sage

The function getDate() returns only date but not time and hence you will get same value for every hour in a day. Try following




    var today = new GlideDateTime();


    var curDate = today.getNumericValue();



uma17
Tera Guru

Hi All,



Thank you for the reply.



Gurpreet's answer was more suitable for my requirement.



Thanks,


Uma