- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 12:17 AM
Hi Everyone,
How to get current date time using client script, Please let me know..
Thank you,
Naveen
Solved! Go to Solution.
- Labels:
-
User Experience and Design

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 02:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 12:27 AM
HI Naveen i am sharing the script based on this u change as your requirement
Script Include
var GetDate = Class.create();
GetDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
GetDate: function(){
var chdate = this.getParameter('sysparm_date');
var date = gs.now();
var diffseconds = gs.dateDiff(date, chdate, true);
return parseInt(diffseconds);
},
type: 'GetDate'
});
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var expdate = g_form.getValue('u_date');
var dajax = new GlideAjax('GetDate');
dajax.addParam('sysparm_name','GetDate');
dajax.addParam('sysparm_date',expdate);
dajax.getXML(DajaxResponse);
function DajaxResponse(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer < 0)
{
alert('Entered date is not valid. Kindly select future date');
g_form.clearValue('u_date');
}}}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 12:33 AM
Hello naveenmyana
Maybe this help you
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var fecha = new Date();
alert(fecha);
}
Ariel
PS: If my answer is correct, please mark it as correct. If you find it helpful, mark it as helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 01:01 AM
Hello Ariel,
Thank you for your response, It is returning the My time zone(IST) but I need the user time zone who is currently logged in.
Here my scenario is whenever a task is created on sc task table and is not closed after 7 days, we need to force the user to select a field on sc task table.
So for this I am taking the time of when the task is created and calculating the duration upto 7 days, So is the task is open for 7 days the user who is working on this task needs to select a reason from a field "XYZ" whenever they are updating that task.
Could pls guide me on this one how can i proceed further..
here is my code:
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.getXML(nowdate);
function nowdate(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
// I am struck here
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2018 12:53 AM
Hi,
THis solution works,
Client script:
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Returns the Date/Time of right now.
getNowDateTime: function(){
var now = gs.nowDateTime(); //Now Date/Time
return now;
},
});
Useful Links:
https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3
THanks,
Ashutosh Munot
Please Hit ✅Correct, ⭐️Helpful depending on the impact of the response