Regular expression - DateTime

Hi all, I just need to confirm that if a datetime entered in exactly in this format…“mm-dd-yyyy hh:mm:ssPM” …example: “02-25-2010 02:26:18AM”

Thank you for help

As you are likely to need to parse a wide range of dates, you can use the Date.parse method for this. Something like the following should suit.


function getDate(date) {
    var hours = 60 * 60 * 1000,
        suffix = date.slice(-2).toUpperCase(),
        timestamp;
    // handle AM/PM then slice it off
    if (suffix === 'AM' || suffix === 'PM') {
        date = date.slice(0, -2);
    } else {
        suffix = '';
    }
    // parse the date
    timestamp = Date.parse(date);
    if (suffix === 'PM') {
        timestamp += 12 * hours;
    }
    // create the date
    return new Date(timestamp);
}
var d = getDate('02-25-20g10');
if (d == 'Invalid Date') {
    // is bad date
} else {
    // is good date
}

Doesn’t look a question… “if a datetime entered in exactly that format”… what?

What language is providing you the datetime - we can source a well-tested script for you based on what created the datetime.

<input type=“text” name=‘d’ value=“02-25-2010 02:26:18AM”>

Yes, but what type of scripting language is creating the value?
Is it from a database? What type?
Is a scripting language like ASP or PHP creating the date?

What is it that is actually responsible for creating the way that the date is formatted?

it is user entered data…

Well something to get started with for the date

/^(?:0\d|1[0-2])\/(?:[0-2]\d|3[01])\/(?:19\d{2}|200\d)$/.test(num);

function isDate(num){ return /^(?:0\d|1[0-2])\/(?:[0-2]\d|3[01])\/(?:19\d{2}|200\d)$/.test(num); } // mm/dd/yyyy

console.log(isDate(“03/05/1973”))// true
console.log(isDate(“03/05/1981”))// true
console.log(isDate(“03/32/1981”))// false
console.log(isDate(“13/31/1971”))// false
console.log(isDate(“11/31/1871”))// false
console.log(isDate(“11/05/2099”))// false
console.log(isDate(“11/29/1973”))// true

I’ll figure out the rest in a bit.

Didn’t know anything about date.parse. Will look into that.

i have found below on the web which confirms leap year as well this format. [ yyyy-mm-dd hh:mm:ssAM/PM] but I need this format [ mm-dd-yyyy hh:mm:ssAM/PM] … I failed to make it work…Any help appreciated.


function Chk_DateTime(input){
if(input.value){	
	if (!input.value.match(/^(((\\d{4})(-)(0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))|((\\d{4})(-)(0[469]|11)(-)([0][1-9]|[12][0-9]|30))|((\\d{4})(-)(02)(-)(0[1-9]|1[0-9]|2[0-8]))|(([02468][048]00)(-)(02)(-)(29))|(([13579][26]00)(-)(02)(-)(29))|(([0-9][0-9][0][48])(-)(02)(-)(29))|(([0-9][0-9][2468][048])(-)(02)(-)(29))|(([0-9][0-9][13579][26])(-)(02)(-)(29)))(\\s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])[AP]M)$/)) { 
			alert ('Please enter a valid DateTime in this format [yyyy-mm-dd hh:mm:ssAM/PM]');
			//input.style.backgroundColor="orange";
			input.style.borderColor="red";
			input.focus(); 
			return false; 
	} 
}
}

function isDate(dateX){ return /^(?:0\\d|1[0-2])\\-(?:[0-2]\\d|3[01])\\-(?:19\\d{2}|20\\d{2})\\s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])[AP]M$/.test(dateX);}

Might need tweaking, I’ve just pretty much bolted on the end of your one to mine.

Comes up true with 02-25-2010 02:26:18AM

It’s wrong though isn’t it, because it checks for a 24 hour clock, in which case why AM and PM.

edit: Amended for a 12 hour clock

function isDate(dateX){ return /^(0\\d|1[0-2])-([0-2]\\d|3[01])-(19\\d{2}|20\\d{2})\\s(0\\d|1[012]):([0-5]\\d):([0-5]\\d)[AP]M$/.test(dateX);}
console.log(isDate("02-25-2010 02:26:18AM")); // true
console.log(isDate("12-30-2012 04:56:18PM")); // true
console.log(isDate("12-30-2012 04:66:18PM")); // false
console.log(isDate("20-12-2012 11:16:18PM")); // false

Might be worth checking out pwm57’s suggestion of date.parse as well.

In addition if may ask, why would you want the user to type in a date down the second?

RLM

Any can tweak the one i posted please to [ mm-dd-yyyy hh:mm:ssAM/PM ] ?

Well this was fun’n’games to work out. Not sure if it’s similar to the one you found.

/^(((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\\d|30))|(02-(0[1-9]|1\\d|2[1-9])))-(19\\d{2}|20\\d{2})\\s(0\\d|1[012]):([0-5]\\d):([0-5]\\d)[AP]M$/

I think it’s right. Should now check exact month lengths e.g. Feb 29 days Nov 30 and Jan 31 days

/^(((0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))(-)(\\d{4})|((0[469]|11)(-)([0][1-9]|[12][0-9]|30))(-)(\\d{4})|((02)(-)(0[1-9]|1[0-9]|2[0-8])(-)(\\d{4}))|((02)(-)(29)(-)([02468][048]00))|((02)(-)(29)(-)([13579][26]00))|((02)(-)(29)(-)([0-9][0-9][0][48]))|((02)(-)(29)(-)([0-9][0-9][2468][048]))|((02)(-)(29)(-)([0-9][0-9][13579][26])))(\\s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])[AP]M)$/

Your original modified to [ mm-dd-yyyy hh:mm:ssAM/PM ]. haven’t checked it yet.

You were looking for (\d{4})'s for the years and a variation on that like ([0-9][0-9][2468][048]) for the leap years

your modified regex failed with this [ 12-15-2010 12:04:44PM ]

function isDate(dateX){ return /^(((0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))(-)(\\d{4})|((0[469]|11)(-)([0][1-9]|[12][0-9]|30))(-)(\\d{4})|((02)(-)(0[1-9]|1[0-9]|2[0-8])(-)(\\d{4}))|((02)(-)(29)(-)([02468][048]00))|((02)(-)(29)(-)([13579][26]00))|((02)(-)(29)(-)([0-9][0-9][0][48]))|((02)(-)(29)(-)([0-9][0-9][2468][048]))|((02)(-)(29)(-)([0-9][0-9][13579][26])))(\\s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])[AP]M)$/.test(dateX);}
console.log(isDate("12-15-2010 12:04:44PM")); // true

No it didn’t ??

The same goes for my previous less strict version.

function isDate(dateX){ return /^(((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\\d|30))|(02-(0[1-9]|1\\d|2[1-9])))-(19\\d{2}|20\\d{2})\\s(0\\d|1[012]):([0-5]\\d):([0-5]\\d)[AP]M$/.test(dateX);}
console.log(isDate("12-15-2010 12:04:44PM")); // true

The difference with the one you supplied is that I think checks for leap years as well.

yes i need to check the leap year as well. And your code below still dont work…with 12-15-2010 12:04:44PM


	function isDate(input){ 
	if (!input.value.match(/^(((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\\d|30))|(02-(0[1-9]|1\\d|2[1-9])))-(19\\d{2}|20\\d{2})\\s(0\\d|1[012]):([0-5]\\d):([0-5]\\d)[AP]M$/));
	alert ('Please enter a valid DateTime in this format [mm-dd-yyyy hh:mm:ssAM/PM]');
	}

And your code below still dont work…with 12-15-2010 12:04:44PM

My regEx does work.

The problem is with your if statement. There’s a semi-colon ; at the end.

It should be if(xy){do this} you have if(xy); do this

function isDate(input){ 
	if (!input.match(/^(((0[13578]|10|12)(-)(0[1-9]|[12][0-9]|3[01]))(-)(\\d{4})|((0[469]|11)(-)([0][1-9]|[12][0-9]|30))(-)(\\d{4})|((02)(-)(0[1-9]|1[0-9]|2[0-8])(-)(\\d{4}))|((02)(-)(29)(-)([02468][048]00))|((02)(-)(29)(-)([13579][26]00))|((02)(-)(29)(-)([0-9][0-9][0][48]))|((02)(-)(29)(-)([0-9][0-9][2468][048]))|((02)(-)(29)(-)([0-9][0-9][13579][26])))(\\s([0-1][0-9]|2[0-4]):([0-5][0-9]):([0-5][0-9])[AP]M)$/))
		{ alert ('Please enter a valid DateTime in this format [mm-dd-yyyy hh:mm:ssAM/PM]'); }
		else { alert("that's fine"); }
	}
isDate("12-15-2010 12:04:44PM");

My apologies Mr.
Thank you sir