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

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', '');  


                }


          }  


    }  


}  


Hi Nayan,



Problem is resolved now. Earlier I wasn't aware of this g_user_date_format property. I googled about this and got more info. Thanks for your response. Issues is fixed now.


@nayanawadhiya Hi,

How to use the above functionality for date/time field?

 

Thanks in Advance!

Mohammed Lais1
Mega Guru

Hi,


        Convert the date string into milliseconds and then validate it.


use this code to change the date in milli seconds.


var field1 = g_form.getValue('start_date');


  var field1_ms = getDateFromFormat(field1, g_user_date_time_format);//in milli seconds