How to display current date using a client script

jmweli
Kilo Expert

Hi ServiceNow Community Developers,

 

If I am looking for todays date i.e. current date I can use gs.NowDateTime on a business rule or on a script include. Any idea how I can get the current date using a client script. Please advise.

 

Thanks,

Johannes

1 ACCEPTED SOLUTION

monkalope
Tera Expert
4 REPLIES 4

monkalope
Tera Expert

Johannes,



Give this a go, Set Current DateTime In Field - ServiceNow Wiki



Regards,


~ Dan


Michael Fry1
Kilo Patron

Try this:


    var ajax = new GlideAjax('MyDateAjax');


    ajax.addParam('sysparm_name','nowDate');


    ajax.getXMLWait();


    var rightnow = ajax.getAnswer();


Jake Gillespie
Mega Guru

Hi Johannes,



If you want to retrieve the date from the client's time settings, you can use a client script with the following:



var today = new Date();


var displayDate = today.getDate()+'-'+today.getMonth()+'-'+today.getFullYear();


OUTPUT: 7-11-2014



The Date class has a heap of methods for setting or getting parts of the date/time. If you open the developer tools Javascript console in Chrome and type "today.", the autocompleter list will show all of the available methods (see the screen cap below as an example).



Screen Shot 2014-12-07 at 7.19.15 am.png



If you need the date value from the server, I would recommend getting the date in a Display Business Rule (e.g using gs.getNowDateTime()), then adding it to the scratchpad (e.g g_scratchpad.todayDate = gs.getNowDateTime().toString(); ) and then retrieving this in your client script (refer to g_scratchpad.todayDate in your client script).



Regards,


Jake


jmweli
Kilo Expert

Hi ServiceNow Developers,



Thank you very much for all your responses, they were all correct. I was actually amazed that this can be done in so many different ways.



Thanks,


Johannes