Compare dates in different format

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2022 10:10 PM
Hi all,
We are looking for a way to compare different date formats. We store date in a string field & then we want to add a validation on server side that if the date is before 02/02/2022 then abort insertion.
But the problem is that user who submits the record in this table, he might have different date format which will cause issues in this date comparison since we have used (dd/MM/yyyy) & user may have date format as (yyyy/MM/dd). Also before hand we don't know user will have which format in his profile.
So what I'm looking for is either a way to compare 2 different date formats or maybe convert any string date format to a common format which helps in date comparison.
Thanks,
Utpal
- Labels:
-
Search
-
User Experience and Design
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 12:26 AM
Hi Utpal,
Then you need convert any data time format into one format for both dates
Check the below link and using the "GlideDateTime" method may be we can achieve this
https://community.servicenow.com/community?id=community_blog&sys_id=bc0e6a2ddbd0dbc01dcaf3231f961931
Please Mark the answer if correct/helpful , if it helps

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 12:56 AM
Hi Utpal,
To validate date on server side, add a hidden field to hold the date format the user used to enter the date.
Set the value of the hidden field to "g_user_date_format". This will set the date format the user is currently using. Use this date format to parse the date on the server-side and then to compare the date.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2022 04:51 AM
Hi Utpal,
Something like the following script. The script will get user's date format and reformat it.
var strDate = '02/02/2022'; // date to check against 'dd/MM/yyyy'
var gdt = convertDateFormat(strDate);
//var currentDate = '2022-02-01'; // current.date to check 'yyyy-MM-dd'
var currentDate = '01/03/2022'; // 'dd/MM/yyyy'
var curGdt = getCurrentDateTime(currentDate);
if (curGdt.getNumericValue() < gdt.getNumericValue()) {
gs.addErrorMessage('please select the date on or after 02/02/2022.');
current.setAbortAction(true); // abort insert
}
gs.info('insert');
function convertDateFormat(strDate) {
var year = strDate.substring(6, 10);
var month = strDate.substring(3, 5);
var day = strDate.substring(0, 2);
var gdt = new GlideDateTime();
gdt.setValue(year + '-' + month + '-' + day);
return gdt;
}
function getCurrentDateTime(currentDate) {
var userDateFormat = String(gs.getSession().getUser().getDateFormat()); // get current user's date format
var yearIndx = getFormatPos(userDateFormat, 'y');
var monthIndx = getFormatPos(userDateFormat, 'M');
var dayIndx = getFormatPos(userDateFormat, 'd');
var year = currentDate.substring(yearIndx[0], yearIndx[1]);
var month = currentDate.substring(monthIndx[0], monthIndx[1]);
var day = currentDate.substring(dayIndx[0], dayIndx[1]);
var curGdt = new GlideDateTime();
curGdt.setValue(year + '-' + month + '-' + day);
return curGdt;
}
function getFormatPos(format, c) {
var start = 0;
var end = 1;
for (var i = 0; i < format.length; i++) {
if (format[i] == c) {
start = i;
break;
}
}
for (var j = format.length; j > 0; j--) {
if (format[j] == c) {
end = j;
break;
}
}
return [start, end + 1];
}