JavascriptToolbox.com Now Available!

Almost all of my javascript code has been moved over to its new home at The Javascript Toolbox. Please go there to find the latest scripts, information, etc. These pages will remain here for a while for historical purposes in case anyone needs a production copy of old code.
Date Functions
[Toolbox]  [Example]  [Source]
Description:
These functions are very useful when dealing with any type of date fields.

They use the same format strings as the java.text.SimpleDateFormat class, with a few minor exceptions.

Functions include:

  • isDate() - Check if a date is valid
  • compareDates() - See if one date is greater than another
  • formatDate() - Output a Date() object in any format
  • getDateFromFormat() - Parse a date string and return time in ms representing date object

Example:
(Note the difference between M (month) and m (minute)!!!)
  Date String Date Format  
1:
2:
Date Format Output Current Date
Date String Date Format Convert To Format Output
Free-Form Date Field Parsed Date
The format string consists of the following abbreviations:

Field        | Full Form          | Short Form
-------------|--------------------|-----------------------
Year         | yyyy (4 digits)    | yy (2 digits), y (2 or 4 digits)
Month        | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
             | NNN (abbr.)        |
Day of Month | dd (2 digits)      | d (1 or 2 digits)
Day of Week  | EE (name)          | E (abbr)
Hour (1-12)  | hh (2 digits)      | h (1 or 2 digits)
Hour (0-23)  | HH (2 digits)      | H (1 or 2 digits)
Hour (0-11)  | KK (2 digits)      | K (1 or 2 digits)
Hour (1-24)  | kk (2 digits)      | k (1 or 2 digits)
Minute       | mm (2 digits)      | m (1 or 2 digits)
Second       | ss (2 digits)      | s (1 or 2 digits)
AM/PM        | a                  |

NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!

Examples:
 "MMM d, y" matches: January 01, 2000
                     Dec 1, 1900
                     Nov 20, 00
 "M/d/yy"   matches: 01/20/00
                     9/2/00
 "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"