Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Change the date format to 'yyyy-mm-dd' even if user enters date manually

aamir1
Mega Expert

Hi,

I need to change the date format to 'yyyy-mm-dd' of a date type field even if user enters date manually.

I tried with getByFormat but it is returning wrong year. Kindly help.

 

Thanks

1 ACCEPTED SOLUTION

Hi,

Please use MM instead of mm, and it will work.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

8 REPLIES 8

Danish6
Giga Expert

Hi Aamir,

It comes from system property 'glide.sys.date_format' which is defined in "sys_properties" table. you can change the value as per your requirement.

 

Utpal Dutta1
Mega Guru

Hey Aamir,

 

Try this onChange Client Script i wrote for Date field Validation.

 

function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == oldValue) {

                return; 

      if (getDateFromFormat(newValue, 'dd-MM-yyyy HH:mm:ss') == 0) { 

                  g_form.hideErrorBox('pstart_date');

                  g_form.showErrorBox('pstart_date', 'Date is invalid');

      }

      else {

                  g_form.hideErrorBox('pstart_date');

      }
}

 

 

If it works mark my answer Correct and Helpful.

 

Thanks and Regards:

Utpal Dutta

Or you can try below script as well in BR.

 

 

(function executeRule(current, previous /*null when async*/) {

      var gd = new GlideDate();

      current.dateprocessed = gd.getByFormat('YYYY-MM-DD');

})(current, previous);

asifnoor
Kilo Patron

Hi,

Here is how you can convert it.

var str = "2020-09-18";
var todayDateTime = new GlideDateTime(str);
var date1= todayDateTime.getLocalDate().getByFormat('MM-dd-yyyy');
gs.print(date1);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP