Tuesday, 29 January 2019

Client Script Date/Time Functions

  1. 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);
}

  1. 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);
  1. 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);

}
  1. 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);
}

  1. 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);

  1. 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);
  1. 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);
  1. 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);
}
  1. 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);
}

  1. 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);
}





No comments:

Post a comment