A JavaScript Library that extends JavaScript's Date object to provide all the functionality that is missing from the standard implementation. With "f" is for Format you can easily output a date in almost any format. WHAT THE diff?? gives you the ability to calculate the exact difference between ANY two dates and output the difference in human readable text. Also included are several other useful Date methods such as Date.isLeapYear and Date.getDaysInMonth.
Date.f
Outputs a JavaScript Date Object in virtually any textual format.
var myDate = new Date(); var myFormattedDate = myDate.f("EE, MMM d, y"); var myFormattedDate2 = myDate.f("MM/dd/yyyyy HH:mm:ss"); alert("Today is: " + myFormattedDate); alert("Or would you prefer something more specific: " + myFormattedDate2);
Demo
The format string uses the following abbreviations:
- Unix Time
- @ (Unix Timestamp)
- Year
- yyyy (4 digits)
yy (2 digits)
y (2 or 4 digits)- Month
- MMM (name or abbr.)
NNN (abbr.)
N (initial)
MM (2 digits)
M (1 or 2 digits)- Day of Month
- dd (2 digits)
d (1 or 2 digits)- Day of Week
- EE (name)
E (abbr)
ee (2 char)
e (initial)
- Hours (1-12)
- hh (2 digits)
h (1 or 2 digits)- Hours (0-23)
- HH (2 digits)
H (1 or 2 digits)- Hours (0-11)
- KK (2 digits)
K (1 or 2 digits)- Hours (1-24)
- kk (2 digits)
k (1 or 2 digits)- Minutes
- mm (2 digits)
m (1 or 2 digits)- Seconds
- ss (2 digits)
s (1 or 2 digits)- AM/PM
- a
Examples:
"MMM d, y"
outputs: "January 01, 2000" or "Dec 1, 1900" or "Nov 20, 00"
"M/d/yy"
outputs: "01/20/00" or "9/2/00"
"MMM dd, yyyy hh:mm:ssa"
outputs: "January 01, 2000 12:30:45AM"
"@"
outputs: "915177604200"
Date.diff
Calculates the exact difference between any two dates, broken down into customizable increments.
var oldDate = new Date("03/22/2009 23:23:12"); var curDate = new Date(); var theDiff = curDate.diff(oldDate); //is the same as var theDiff = curDate.diff(oldDate, "TCDYMWdHmSN"); alert("And the difference is: " + theDiff);
Demo
The breakdown string uses the following abbreviations:
- T
- Millenia
- C
- Centuries
- D
- Decades
- Y
- Years
- M
- Months
- W
- Weeks
- d
- Days
- H
- Hours
- m
- Minutes
- S
- Seconds
- N
- Miliseconds
- *
- All (default)
Date.getDaysInMonth
Returns the number of days in the selected monthvar thisMonth = new Date(); var daysThisMonth = thisMonth.getDaysInMonth(); alert("There are " + daysThisMonth + " days in this month.");
Date.isLeapYear
Returns true if the selected year is a leap yearvar thisYear = new Date(); if(thisYear.isLeapYear()) { alert("It's a leap year!"); } else { alert("Maybe next year..."); thisYear.setYear(thisYear.getYear() + 1); if(thisYear.isLeapYear()) { alert("Yes!!"); } else { alert("Darn."); } }
Date.isDayLightSavingsDay
Returns true if the selected day is a daylight savings change dayvar tomorrow = (new Date()).setDate((new Date()).getDate() + 1); if(tomorrow.isDayLightSavingsDay()) { alert("Don't forget to change the clock tonight!"); }
Date.getDayLightSavingsDays
Returns an array containing date objects for the two daylight savings change days within the selected yearvar thisYear = new Date(); alert( "On " + thisYear.getDayLightSavingsDays[0].f("DD, MMM d") + " spring forward.\n" + "On " + thisYear.getDayLightSavingsDays[1].f("DD, MMM d") + " fall back." );