- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 07:41 PM
Hi,
In my script include "date" is a input value and need to check the date format [ yyyy-mm-dd , mm/dd/yyyy] in script include . Please help to check the date format.
Regards
Sagaya
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 09:06 PM
Hi Sagaya,
Can you explain a bit more what you want to do? You want to check if it is either of those 2 formats?
You can use the following:
var inputDate = '';//put date value here
var regexp = /^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/;//regular expression for yyyy-mm-dd
var regexp2 = /^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$/;//regular expression for mm/dd/yyyy
if (!regexp.test(inputDate) && !regexp2.test(inputDate)) {
//does not match either format
//run code if no match here
}
else {
//run code for match here
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 09:50 PM
Following script will check if the date is yyyy/mm/dd or mm/dd/yyyy.
var date2Check= '2019/12/31';
var pattern = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
var dateArray = date2Check.split('/');
var result = true;
if (dateArray.length != 3) {
result = false;
} else {
var yyyymmdd = dateArray[2] + '/'+dateArray[1] + '/' + dateArray[0];
var mmddyyyy = dateArray[1] + '/' + dateArray[0] + '/' + dateArray[2];
if (pattern.test(yyyymmdd)) {
gs.info('is yyyy/mm/dd');
} else if (pattern.test(mmddyyyy)) {
gs.info('is mm/dd/yyyy');
} else {
gs.info('invalid date');
result = false;
}
}
gs.info(result);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 09:55 PM
BTW, following will get current date time format.
gs.getSession().getUser().getDateFormat()
