How to get current date time using client script???

naveenmyana
Giga Expert

Hi Everyone,

How to get current date time using client script, Please let me know..

 

Thank you,

Naveen

 

1 ACCEPTED SOLUTION

Hi,

 

I gave you one link in my reply. USEFUL LINK.

 

There you can see various ways to do this.


Example is as below:

Client script:

var cdt = g_form.getValue('due_date'); //First Date/Time field
var dttype = 'second'; //this can be day, hour, minute, second. By default it will return seconds.

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer > 604800 )
{
g_form.setMandatory('fieldname',true);
}
}


Script include:

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a Single Date/Time Field and returns its time difference from nowDateTime().
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},

//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
}


});


Thanks,
Ashutosh Munot

 

Please Hit Correct, ️Helpful depending on the impact of the response

 

View solution in original post

15 REPLIES 15

I tried the above suggested code and it did not work. I could not get the current date/time.

Am i missing something?

Obul
Mega Guru

Please see the below URL.

 

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

 

Client Script Date/Time Functions

 

Shweta KHAJAPUR
Tera Guru

Hi,

Use any business rule to get current date time in g_scratchpad , and you can use this variable in any client script.

In Business rule write below code,

g_scratchpad.dateTime=gs.nowDateTime();

 

In client script use below code,

var date=g_scratchpad.dateTime;

 

current date and time will be captured in date variable.

Trupti6
Tera Expert

Hi,

Please check the below script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var approveddate = g_form.getValue('request_item.u_approved_date');
// alert(approveddate);

var dttype = 'seconds';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');

ajax.addParam('sysparm_date',approveddate );
ajax.getXML(nowdate);


function nowdate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
 // I am struck here
}
}

 

Script include:

 

add this code to your script include

 

var current=gs.nowDateTime();

var date=this.getParameter('getNowDateTime');

var date_difference =gs.calDateDiff('date','current',true);// this method gives the you difference in seconds try to convert into hours

or days..

 

 

Thanks,

Hope so this will help you 🙂

 

Dave49
Giga Contributor

Very simple solution:

var today_date = new Date();
var today_date_time_str = formatDate(today_date, g_user_date_time_format);
g_form.setValue('u_escalation_date', today_date_time_str);

 

g_user_date_time_format is the key to getting the date and time in the correct format.

If you only need the date use: g_user_date_format