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  

Date format check in script include

Sagaya1
Giga Expert

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

 

 

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

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
}

View solution in original post

6 REPLIES 6

Hitoshi Ozawa
Giga Sage
Giga Sage

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);

Willem
Giga Sage
Giga Sage

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
}

Hi Willem ,

Thank you for your help and its working now.

 

Regards

Sagaya.

RupeshM
Tera Contributor

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