The CreatorCon Call for Content is officially open! Get started here.

How to convert Date/Time to standard format for validating

mallikarjunared
Tera Contributor

Hi All,

We need to get the date value from the catalog variable and validate it in such a way that it should not allow user to submit the request for past dates. I have written the code, it is working fine only for system format. (YYYY-MM-DD). Below are the formats available in user profile. Based on user selected format it will be available the same in catalog date fields also.

find_real_file.png

Irrespective of the date format, we should not allow the user to submit the request for past dates.

Script Include:

validateDate: function()

  {

  var gdt = new GlideDateTime(gs.now());

  var due_date = this.getParameter('sysparm_user_name');

  gs.log("now date:"+gdt+" due date:"+due_date);

  if(due_date> gdt)

  {

  gs.log("inside loop");

  return "true";

  }

Client Script On Change

var ga = new GlideAjax('validateDate');

  ga.addParam('sysparm_name','validateDate');

  ga.addParam('sysparm_user_name', newValue);

  ga.getXML(DateParse);

  function DateParse(response)

  {

  var answer = response.responseXML.documentElement.getAttribute("answer");

  if(answer != 'true')

  {

  alert('Past dates are not allowed to submit the request');

  g_form.setValue('Requested_Date_Due','');

  }

  }

Any suggestions highly appreciable

1 ACCEPTED SOLUTION

nayanawadhiya1
Kilo Sage

Hey Mallikarjuna,



Use this instead of script include -


function onChange(control, oldValue, newValue, isLoading) {  


    if (!isLoading) {  


          if(newValue != '') {  


                //current date  


                var currentDateObj = new Date();  


                var currentDateStr = formatDate(currentDateObj, g_user_date_format);  


                var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);  


 


//get start date  


var startDateStr = g_form.getValue('sDate');  


var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);  


                               


                if (startDateNum < currentDateNum) {  


                      alert('You cannot select a date in the past.');  


                      g_form.setValue('sDate', '');  


                }


          }  


    }  


}  


View solution in original post

6 REPLIES 6

Harish Murikina
Tera Guru

Hi Mallikarjuna,



Here you go.




Script include


____________





var ValidateDate = Class.create();


ValidateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


                           


                           


                           


                              validatePastDate : function(number0fdays)


                              {


                                                              try


                                                              {


                                                                                           


                                                                                              var format ='';


                                                                                           


                                                                                              var timeformat = new GlideRecord('sys_user');


                                                                                              timeformat.addQuery('sys_id', gs.getUserID());


                                                                                              timeformat.query();


                                                                                              if(timeformat.next())


                                                                                                                              {


                                                                                                                              format = timeformat.date_format;


                                                                                                                              }


                                                                                           


                                                                                              var days_ago = this.getParameter('sysparm_user_name'); // this should be your data and time type   and hope this is the date time variable   name "sysparm_user_name"


                                                                                              var splitdate = days_ago.split(' ');


                                                                                              var onlysplitdate = splitdate[0].split('-');


                                                                                              var year = onlysplitdate[0];


                                                                                              var month = onlysplitdate[1];


                                                                                              var date = onlysplitdate[2];


                                                                                              var day='';


                                                                                              if(JSUtil.nil(format))


                                                                                                                              {


                                                                                                                           


                                                                                                                              format = gs.getProperty('glide.sys.date_format');


                                                                                                                                                           


                                                                                              }


                                                                                           


                                                                                              if(format == 'MM-dd-yyyy')


                                                                                                                              {


                                                                                                                              day = month+'-'+date+'-'+year+' '+splitdate[1];


                                                                                              }


                                                                                           


                                                                                              else if(format == 'dd/MM/yyyy')


                                                                                                                              {


                                                                                                                           


                                                                                                                              day = date+'/'+month+'/'+year+' '+splitdate[1];


                                                                                              }


                                                                                           


                                                                                              else if(format == 'dd-MM-yyyy')


                                                                                                                              {


                                                                                                                              day = date+'-'+month+'-'+year+' '+splitdate[1];


                                                                                              }


                                                                                           


                                                                                              else if(format == 'dd.MM.yyyy')


                                                                                                                              {


                                                                                                                              day = date+'.'+month+'.'+year+' '+splitdate[1];


                                                                                              }


                                                                                              else if(format == 'yyyy-MM-dd')


                                                                                                                              {


                                                                                                                              day = year+'-'+month+'-'+date+' '+splitdate[1];


                                                                                              }






                                                                                         


var a = day;


                                                              var b = gs.nowDateTime();


                                                              var answer = gs.dateDiff(b,a,true);


                                                                                           


                                                                                           


                                                              }


                                                              catch(err)


                                                              {


                                                                                              gs.log('Error while getting the date :'+err.message);


                                                                                           


                                                              }


                                                           


                                                           


                                                              return answer,


                              },


                           


                             


                           


                              type: 'ValidateDate'


});







Client script


________________________



var ga = new GlideAjax('ValidateDate');


  ga.addParam('sysparm_name','validatePastDate');


  ga.addParam('sysparm_user_name', newValue);


  ga.getXML(DateParse);


  function DateParse(response)


  {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if(answer != 'true')


  {


  alert('Past dates are not allowed to submit the request');


  g_form.setValue('Requested_Date_Due','');


  }


  }



Regards,


Harish Murikinati.


Hi Harish,



Let me check this.



Thanks for your quick response.