NavigationLibrariesdoctype [?][strict] [loose] [none] |
Date Formatting And Format Validation |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Format Reference
FormattingEnter a format string in the input box and click the button to see the resulting output in an alert box. ParsingEnter 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. ComparingThe 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 TimeAdd any amount of time (including negative!) to a date and see the newly calculated 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>
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
All Contents Copyright © Matt Kruse |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||