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

 

Hitoshi Ozawa
Giga Sage
Giga Sage

BTW, following will get current date time format.

gs.getSession().getUser().getDateFormat()