I am using MaskedEditExtender for entering a datetime. I am unable to figure out how to validate it.
Is there any Regular Expression for validating dates along with time MM/dd/yyyy hh:mm
or any Javascript function ??
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
THis will solve your issue:
^(([0]?[1-9]|1[0-2])/([0-2]?[0-9]|3[0-1])/[1-2]d{3}) (20|21|22|23|[0-1]?d{1}):([0-5]?d{1})$
Method 2
Javascript has Date.parse
it takes US formatted date of mm/dd/yyyy hh:mm:ss and with that format it works in all browsers I have tested: Firefox, Safari, Chrome, Edge
console.log(new Date(Date.parse("03/25/2022 12:00")))
will return 10th September 2011 at noon
Method 3
Use DateTime.Parse or DateTime.TryParse (there are also ParseExact and TryParseExact equivalents).
If the string does not represent a valid DateTime it will not parse.
DateTime myDateTime = DateTime.ParseExact(myString,
"MM/dd/yyyy hh:mm",
CultureInfo.InvariantCulture);
The above will throw an exception if the value is not parseable. Use the Try variant if you want to avoid the chance of the exception being thrown – this requires an out parameter and testing the return value of the function for success.
Method 4
And just in case you want the regular expression, this should work:
^(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)dd ([01]d|2[0-3]):[0-5]d$
Method 5
The following Regex:
^([1-9]|([012][0-9])|(3[01]))/([0]{0,1}[1-9]|1[012])/([1-2][0-9][0-9][0-9]) [0-2][0-9]:[0-9][0-9]
gives this result:
03/03/2021 02:12
Method 6
You may try following Function that Validates date in “dd/MM/yyyy HH:mm” format
function ValidateDate(dt) {
try {
var isValidDate = false;
var arr1 = dt.split('/');
var year=0;var month=0;var day=0;var hour=0;var minute=0;var sec=0;
if(arr1.length == 3)
{
var arr2 = arr1[2].split(' ');
if(arr2.length == 2)
{
var arr3 = arr2[1].split(':');
try{
year = parseInt(arr2[0],10);
month = parseInt(arr1[1],10);
day = parseInt(arr1[0],10);
hour = parseInt(arr3[0],10);
minute = parseInt(arr3[1],10);
//sec = parseInt(arr3[0],10);
sec = 0;
var isValidTime=false;
if(hour >=0 && hour <=23 && minute >=0 && minute<=59 && sec >=0 && sec<=59)
isValidTime=true;
else if(hour ==24 && minute ==0 && sec==0)
isValidTime=true;
if(isValidTime)
{
var isLeapYear = false;
if(year % 4 == 0)
isLeapYear = true;
if((month==4 || month==6|| month==9|| month==11) && (day>=0 && day <= 30))
isValidDate=true;
else if((month!=2) && (day>=0 && day <= 31))
isValidDate=true;
if(!isValidDate){
if(isLeapYear)
{
if(month==2 && (day>=0 && day <= 29))
isValidDate=true;
}
else
{
if(month==2 && (day>=0 && day <= 28))
isValidDate=true;
}
}
}
}
catch(er){isValidDate = false;}
}
}
return isValidDate;
}
catch (err) { alert('ValidateDate: ' + err); }
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0