Logo
doctype [?]
[strict] [loose] [none]

Date Formatting And Format Validation


Format Reference


Field Syntax Parse Format
Yearyyyy4 digits4 digits
Yearyy2 digits2 digits
Yeary2 or 4 digits4 digits
MonthMMMName or AbbreviationName
MonthNNNAbbreviationAbbreviation
MonthMM2 digits (01-12)2 digits (01-12)
MonthM1 or 2 digits (1-12)1-2 digits (1-12)
Day of Monthdd2 digits (01-31)2 digits (01-31)
Day of Monthd1 or 2 digits (1-31)1-2 digits (1-31)
Day of WeekEEFull name (Sunday-Saturday)Full name
Day of WeekEAbbreviation (Sun-Sat)Abbreviation
Hourhh2 digits (01-12)2 digits (01-12)
Hourh1 or 2 digits (1-12)1-2 digits (1-12)
HourHH2 digits (00-23)2 digits (00-23)
HourH1 or 2 digits (0-23)1-2 digits (0-23)
HourKK2 digits (00-11)2 digits (00-11)
HourK1 or 2 digits (0-11)1-2 digits (0-11)
Hourkk2 digits (01-24)2 digits (01-24)
Hourk1 or 2 digits (1-24)1-2 digits (1-24)
Minutemm2 digits (00-59)2 digits (00-59)
Minutem1 or 2 digits (0-59)1-2 digits (0-59)
Secondss2 digits (00-59)2 digits (00-59)
Seconds1 or 2 digits (0-59)1-2 digits (0-59)
AM/PMaAM/am/PM/pmAM/PM

Formatting

Enter a format string in the input box and click the button to see the resulting output in an alert box.

Parsing

Enter a date string in the input box and click the parseString button to see the resulting Date object in an alert box. Click the isValid button to see the true/false result of the isValid method. Try leaving the format value blank to parse the built-in default formats.

Date: Format:
Date: Format:

Comparing

The examples below all use the built-in parsing formats which can be found in the documentation.

<view/hide code>
<script type="text/javascript">
function isBefore(f) {
	if (!Date.isValid(f.date1.value)) {
		alert("Date 1 is not a valid date or in a recognized format!"); return;
	}
	if (!Date.isValid(f.date2.value)) {
		alert("Date 2 is not a valid date or in a recognized format!"); return;
	}
	alert(Date.parseString(f.date1.value).isBefore(Date.parseString(f.date2.value)));
}
</script>


<view/hide code>
<script type="text/javascript">
function isAfter(f) {
	if (!Date.isValid(f.date1.value)) {
		alert("Date 1 is not a valid date or in a recognized format!"); return;
	}
	if (!Date.isValid(f.date2.value)) {
		alert("Date 2 is not a valid date or in a recognized format!"); return;
	}
	alert(Date.parseString(f.date1.value).isAfter(Date.parseString(f.date2.value)));
}
</script>


<view/hide code>
<script type="text/javascript">
function isEqual(f) {
	if (!Date.isValid(f.date1.value)) {
		alert("Date 1 is not a valid date or in a recognized format!"); return;
	}
	if (!Date.isValid(f.date2.value)) {
		alert("Date 2 is not a valid date or in a recognized format!"); return;
	}
	alert(Date.parseString(f.date1.value).equals(Date.parseString(f.date2.value)));
}
</script>


Adding Time

Add any amount of time (including negative!) to a date and see the newly calculated date/time.

Starting Date/Time January 1, 2000 1:00:00 AM (Saturday)
Add Years
Months
Days
Weekdays
Hours
Minutes
Seconds
 
Ending Date/Time
<view/hide code>
<script type="text/javascript">
function addTime(f) {
	var format = "MMM d, yyyy h:mm:ss a (EE)";
	var date1 = new Date(2000,0,1,1);
	var date2 = f.elements['date2'];
	if (f.y.value>0 || f.y.value<0) {
		date1.add('y',f.y.value);
	}
	if (f.M.value>0 || f.M.value<0) {
		date1.add('M',f.M.value);
	}
	if (f.d.value>0 || f.d.value<0) {
		date1.add('d',f.d.value);
	}
	if (f.w.value>0 || f.w.value<0) {
		date1.add('w',f.w.value);
	}
	if (f.h.value>0 || f.h.value<0) {
		date1.add('h',f.h.value);
	}
	if (f.m.value>0 || f.m.value<0) {
		date1.add('m',f.m.value);
	}
	if (f.s.value>0 || f.s.value<0) {
		date1.add('s',f.s.value);
	}
	date2.value = date1.format(format);
}
</script>