- 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 08:59 PM
Following will return true when valid including leap year check.
var date2Check= '13/30/2019'; // date to check. either yyyy/mm/dd or mm/dd/yyyy
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) && !pattern.test(mmddyyyy)) {
result = false;
}
}
gs.info(result);
- 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 10:43 PM
Hi Willem ,
Thank you for your help and its working now.
Regards
Sagaya.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:54 AM
How about DateTime UTC regular expression
YYYY-MM-DDThh:mm:ssZ.
I tried this ^(\d{4})\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])T([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])Z$ but not worked