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

Hi Ashutosh,

 

I have just tried the same but I need to do some more work here.

I need to compare this current date with the date of creation of a sctask on sc_task table. so if the difference between current date and date of creation of sctask is more than 7 days I need to  make a field mandatory on sctask table.

 

Any suggestions where I can add this bit of code there.

My code---

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

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

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');
ajax.getXML(nowdate);


function nowdate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
var diff = gettimeDatediff(approveddate-answer);

// I am struck here
}
}

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

 

Did you try this code. It will work?

 

Just  instead of 

var cdt = g_form.getValue('due_date');

you have to write :
var cdt = g_form.getValue('sys_created_on'); // This should come from, when the task is created.

Thanks,
Ashutosh Munot

Hi,

Did this work?


Thanks,

Ashutosh Munot

Hi Ashutosh Munot

Can you help me in validating catalog date/time field to alert user not to select past date and time. I am able to achieve for date field but not success for date/time field.

Thanks in advance