- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 10:24 PM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 11:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 10:34 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 10:34 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 10:41 PM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2020 10:36 PM
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