Client Script Date/Time Functions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2011 02:33 PM
This post started out with me trying to compare glide date/time fields via client scripts, which can be a bear. Since gs. is not available in the client, I was forced to come up with other methods of manipulating these fields via a client script. Using g_form.getNumericValue(), then parsing out the year, month, day, time etc. and putting that into a new Date() javascript object was one among many options I saw. However this method does not take into account differences in date/time formats.
The method I decided to use was Asynchronous Glide Ajax. Forget the "AJAXEvaluateSynchronously" function of the past. With GlideAjax you can query for information asynchronously not preventing the browser from continuing to respond, unlike the other. Big win! So I set out to build a "ClientDateTimeUtils" script include that a glideajax script could call.
I am hoping to continue to improve these scripts, add more functionality, etc. If you have any suggestions, please let me know. A common ClientDateTimeUtils script would be nice to have.
The actual script include can be way found below. These are the functions, and an associated glideajax examples, of what you can call with this.
- getNowDateTimeDiff
This ajax function will allow you to calculate the date/time difference between a field on the form and the now date/time. You can specify a return type - "dttype" to get the result in seconds, minutes, hours, or days. See the comment below for the parameter values.
var cdt = g_form.getValue('due_date'); //First Date/Time field
var dttype = 'minute'; //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);
} - getDateTimeDiff
This ajax function will allow you to send in two different glide date/time fields and return a calculation of their date/time difference in seconds, minutes, hours, or days.
var cdt = g_form.getValue('due_date'); //First Date/Time field
var sdt = g_form.getValue('expected_start'); //Second Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
} - getDateTimeBeforeNow
This ajax function will take a glide date/time field and return the amount of time till the now date/time. A positive number will represent prior to now, and negative will be how much time after now. This is pretty much a duplicate of the first one "getNowDateTimeDiff" but it gives you the opposite in positive, negative numbers. Positive = before, negative = after.
var cdt = g_form.getValue('due_date'); //first Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNow');
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);
} - getDateTimeBeforeNowBool
This ajax function will return a true or false if the date/time field is before the now date/time. This made it simpler, rather than having to do more processing on the client side, just evaluate true/false. Makes it simple.
var cdt = g_form.getValue('due_date'); //first Date/Time field
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNowBool');
ajax.addParam('sysparm_fdt', cdt);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
} - getNowDateTime
This function returns the date and time of right now.
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); - getNowDate
This function returns the date of right now.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDate');
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); - getNowTime
This function returns the time of right now.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowTime');
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); - addDateTimeAmount
With this ajax function you can add more time to a glide date/time field. You can add seconds, minutes, hours, days, weeks, months, and years. If you want to take time away, use a negative number in the addtime variable.
Limitation:
One limitation that I have found so far is in the return date/time format. The added-to-time new date/time is returned in the standard internal date/time format. I am trying to figure out how to return it based on the user defined date/time format. Still a work in progress.
var cdt = g_form.getValue('due_date'); //Choose the field to add time from
var addtime = 3; //The amount of time to add
var addtype = 'day'; //The time type. Can be second, minute, hour, day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//You could then take the new Date/Time answer and set the value of another field.
// g_form.setValue('expected_start', answer);
alert(answer);
} - addDateAmount
This is a function to add time to a Glide Date field. You can add days, weeks, months, or years.
var cdt = g_form.getValue('some_date'); //Choose the field to add time from
var addtime = 3; //The amount of time to add
var addtype = 'day'; //The time type. Can be day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
} - addTimeAmount
This is a function to add time to a Glide Time field. You can add seconds, minutes, hours.
var cdt = g_form.getValue('some_time_field'); //Choose the field to add time from
var addtime = 3; //The amount of time to add
var addtype = 'day'; //The time type. Can be second, minute, hour.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addTimeAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
ClientDateTimeUtils Script Include Code
I have not found that there is any limitation with the date or time formats a user may use in the calculation of the dates/times on the server. I have tested each one of these in many different timezones and date/time formats with consistent success in their calculation. The only limitation that I have found is listed above in the "addDateTimeAmount" function. i.e. The returned date/time is in the standard internal format, not the user defined format. One other point of variation would be that all of my testing was done with Glide Date/Time fields. I have not tested merely Glide Date or Time fields.
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;
},
//Diff the amount of time between two different Date/Time fields
//params = sysparm_fdt (the first date/time field), sysparm_sdt (second date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var secondDT = this.getParameter('sysparm_sdt'); // Second 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(firstDT, secondDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Takes your date/time field and returns the amount of time before now. A positive is time before now, a negative number is after now.
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getDateTimeBeforeNow: 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(firstDT, gs.nowDateTime(), true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getDateTimeBeforeNow: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Returns true if it is before now, and false if it is after now.
//params = sysparm_fdt (the first date/time field)
getDateTimeBeforeNowBool: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diff = gs.dateDiff(firstDT, gs.nowDateTime(), true);
var answer = '';
if (diff >= 0){answer = 'true';}
else {answer = 'false';}
return answer;
},
//Returns the Date/Time of right now.
getNowDateTime: function(){
var now = gs.nowDateTime(); //Now Date/Time
return now;
},
//Returns the Date right now.
getNowDate: function(){
var now = GlideDate();; //Now Date
return now.getLocalDate();
},
//Returns the Time of right now.
getNowTime: function(){
var now = GlideTime();; //Now Time
var modnow = now.getLocalTime().toString().split(' ');
return modnow[1];
},
//Takes a date/time field and adds time to it.
//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - second, minute, hour, day, week, month, year), sysparm_addtime (amount of time to add based on the type).
addDateTimeAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDateTime(firstDT);
if(addTYPE == 'second'){day.addSeconds(addTIME);}
else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}
else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}
else if (addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},
//Takes a glide date field and adds time to it.
//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - day, week, month, year),sysparm_addtime (amount of time to add based on the type).
addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setValue(firstDT);
if(addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},
addTimeAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var addTYPE = this.getParameter('sysparm_addtype'); //What
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var time = GlideTime();
time.setValue(firstDT);
if(addTYPE == 'second'){time.addSeconds(addTIME);}
else if (addTYPE == 'minute'){time.addSeconds(addTIME*60);}
else if (addTYPE == 'hour'){time.addSeconds(addTIME*(60*60));}
else {time.addSeconds(addTIME);}
var modtime = time.toString().split(' ');
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + time;
return modtime[1];
},
//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;
}
});
- 183,616 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2015 07:46 AM
I could not find anywhere to add an attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-29-2015 08:36 AM
Thank you ... this is very helpful, however I wounder why there is no OOTB such Utils or may be we don't know about it's existance*)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-17-2015 06:45 AM
Thanks very much for posting this Allister.
I'm looking to make a modification to the section "//Takes a Single Date/Time Field and returns its time difference from nowDateTime().", but unsure how to achieve it. When comparing nowDateTime() to a date on the form, it would be ideal to not count Saturdays or Sundays.
Example: If I wanted to add 4 days to a Thursday, the date I would like to compare my form date to would be the following Tuesday.
I'm calling the above script include from the following client script:
//For New User Requests alert the requester if the start date is less than 5 days from now.
//They can still submit the request, but they will have seen the alert and need to click OK to proceed.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
g_form.hideErrorBox('requested_by_date');//clear any Start Date error messages
return;
}
g_form.hideErrorBox('requested_by_date');//clear the previous Start Date error message
var ajax2 = new GlideAjax('MyDateTimeAjax');
ajax2.addParam('sysparm_name','nowDateTime');
ajax2.getXMLWait();
var rightnow = ajax2.getAnswer();
if (newValue != oldValue){
}
var format = g_user_date_time_format;
var startDate = g_form.getValue('variables.requested_by_date');
var addtime = 4;
var ajax = new GlideAjax('ClientDateTimeUtils');//Calls the Script Include ClientDateTimeUtils
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt',startDate);
ajax.addParam('sysparm_addtime',addtime); //How much time to add
ajax.addParam('sysparm_difftype','day');
ajax.getXMLWait();
var diff = ajax.getAnswer();
if (diff < addtime) {
alert("New User Requests should be submitted 5 days in advance. Failure to do so may result in delays for new user accounts, permissions and hardware. Please click OK to acknowledge you understand and accept these terms. Contact the Service Desk for clarification if needed.");
g_form.showErrorBox('requested_by_date', 'New User Requests should be submitted 5 days in advance. Failure to do so may result in delays for new user accounts, permissions and hardware.');
} else {
g_form.hideErrorBox('requested_by_date');//clear any Start Date error messages
}
}
We don't leverage schedules currently and not something we're looking to implement yet.
If anyone has suggestions how I can exclude weekend days in the calculation, I would really appreciate any guidance.
Thanks in advance
Cheers
Ron
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-17-2016 07:08 AM
Hi All,
I was struggling to find a script that would give me the day of week (1 to 7) using a Client Script, and finally got it to work using this ClientDateTimeUtils script include.
Thought I would share it with you if it can ease some headaches.
Lines to add in the ClientDateTimeUtils:
- //Returns the Day of Week
- getDayOfWeek: function(){
- var firstDT = this.getParameter('sysparm_fdt'); // Date-Time Field
- var day = GlideDateTime();
- day.setValue(firstDT);
- var dayofweek = day.getDayOfWeekLocalTime();
- return dayofweek;
- },
Lines to add in the Client script:
- var cdt = g_form.getValue('date_field'); //first Date/Time field
- var ajax = new GlideAjax('ClientDateTimeUtils');
- ajax.addParam('sysparm_name','getDayOfWeek');
- ajax.addParam('sysparm_fdt', cdt);
- ajax.getXML(doSomething);
- function doSomething(response){
- var answer = response.responseXML.documentElement.getAttribute("answer");
- alert(answer);
- }
Hope this helps.
Harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2016 09:58 AM
Hi,
Great post and very in-depth!
I have an issue where when setting 'ajax.addParam('sysparm_name', 'addDateTimeAmount'); then the returns is YYYY-MM-DD 00:00:00.
I have tried using the addType as 'day', 'minute' etc and although the days i am adding works out correct (even if it is in the wrong format) it always returns the time as 00:00:00, when i would like the days to go up and the time to stay the same as the original field.
Any ideas on why this maybe and how to resolve?
Thanks!
Simon
