![](http://datejs.com/images/googlecode-header.jpg)
![](http://datejs.com/images/googlecode-header.gif)
Overview
Datejs is an open source JavaScript Date library for parsing, formatting and processing.
The last 'official' release was Alpha-1 on November 19th, 2007. The project has been mostly dormant since that early release, but steps are being taken to revive the project.
Getting Started
We recommend including one of the .js files from within the /build/
folder.
<script type="text/javascript" src="date.js"></script>
Within the /build/
folder, a date.js file has been created for each of the 150+ supported Cultures. Changing the Culture of the library is as easy as changing the date.js
file.
<script type="text/javascript" src="date-de-DE.js"></script>
Before minification, the Datejs library is contained in five (6) separate JavaScript files. Each of the files can be included individually.
The following is a list of precedence if including the files individually. Each file requires the one above it. For example, core.js
requires a CultureInfo
file.
1. CultureInfo Contains all Globalized strings and culture specific properties.
Debug versions available within the /trunk/source/globalization/ folder.
2. core.js All core Date processing logic.
3. parser.js All parsing logic.
4. sugarpak.js* All syntactical sugar.
5. time.js** TimeSpan and TimePeriod classes.
6. extras.js** PHP/Unix date format conversion functions.
* The parser.js file is not required for sugarpak.js
** The time.js and extras.js files are optional and are not included in the compiled /build/ versions.
Example Usage
Syntax Overview
Date.today()
Date.today().next().friday()
Date.today().last().monday()
new Date().next().march()
new Date().last().week()
Date.today().is().friday()
Date.today().is().fri()
Date.today().is().november()
Date.today().is().nov()
Date.today().is().weekday()
Date.today().addDays(1)
Date.today().addMonths(-3)
Date.today().add(1).day()
Date.today().add(-3).months()
(1).day().fromNow()
(3).months().ago()
var n = 6;
n.months().fromNow()
Date.monday()
Date.mon()
Date.march()
Date.mar()
Date.today().first().thursday()
Date.today().second().thursday()
Date.march().third().thursday()
Date.october().fourth().sunday()
Date.today().fifth().sunday()
Date.october().final().sunday()
Date.january().first().monday()
Date.december().final().friday()
Date.today().at("6:15pm");
var time = {hour:18, minute:15};
Date.today().at(time);
var birthDayParty = {month: 1, day: 20, hour: 20, minute: 30};
Date.today().set(birthDayParty);
Parsing
The following list is only a small subset of hundreds of string formats which can be parsed correctly without providing a date format. All parsing is fully Globalized by including the appropriate CultureInfo file. The CultureInfo file contains all the strings used for parsing and formatting.
All CultureInfo files can be found in the /trunk/source/globalization/
folder.
The following .parse()
samples use the en-US.js
CultureInfo file.
Date.parse('t')
Date.parse('today')
Date.parse('tomorrow')
Date.parse('yesterday')
Date.parse('next friday')
Date.parse('last monday')
Date.parse('July 8th, 2004')
Date.parse('15-Jan-2004')
Date.parse('7/1/2004')
Date.parse('7.1.2004')
Date.parse('07.15.04')
Date.parse('July 23rd 2004')
Date.parse('Sat July 3, 2004')
Date.parse('10:30 PM EST')
Date.parse('10PM')
Date.parse('t + 5d')
Date.parse('today - 1 month')
Date.parse('+')
Date.parse('- 3months')
Date.parse('+1year')
Date.parse('-12 months')
Date.parse('July 4th')
Date.parse('15')
Date.parse('July 8th, 2004, 10:30 PM')
Date.parse('2004-07-15T06:45:00')
Date.parse('Thu, 1 July 2004 22:30:00 GMT')
Date.parse('1997-07-16T19:20:15')
Date.parse('1997-07-16T19:20:30+01:00')
Date.parse('1985-04-12T23:20:50Z')
Chaining
Date.today().add({ months: 1, days: 5 }).is().fri()
Date.parse('10-July-2004').next().friday().add(-1).month()
Comparison
Date.today().equals( Date.parse('today'))
Date.parse('last Tues').equals(Date.today())
Date.equals(Date.today(), Date.parse('today'))
Date.compare(Date.today(), Date.parse('today'))
Date.today().compareTo(Date.parse('yesterday'))
Date.today().between(startDate, endDate)
Converting to String
Note The format
parameter is optional with the .toString()
function. If no format
is provided, the native JavaScript Date .toString()
function will be called.
A detailed list of supported FormatSpecifiers is listed in the [http://code.google.com/p/datejs/wiki/FormatSpecifiers Wiki] documentation.
Standard Date and Time Format Specifiers
Format | Description | Example |
---|
s | The seconds of the minute between 0-59. | "0" to "59" |
ss | The seconds of the minute with leading zero if required. | "00" to "59" |
m | The minute of the hour between 0-59. | "0" or "59" |
mm | The minute of the hour with leading zero if required. | "00" or "59" |
h | The hour of the day between 1-12. | "1" to "12" |
hh | The hour of the day with leading zero if required. | "01" to "12" |
H | The hour of the day between 0-23. | "0" to "23" |
HH | The hour of the day with leading zero if required. | "00" to "23" |
d | The day of the month between 1 and 31. | "1" to "31" |
dd | The day of the month with leading zero if required. | "01" to "31" |
ddd | Abbreviated day name. Date.!CultureInfo.abbreviatedDayNames. | "Mon" to "Sun" |
dddd | The full day name. Date.!CultureInfo.dayNames. | "Monday" to "Sunday" |
M | The month of the year between 1-12. | "1" to "12" |
MM | The month of the year with leading zero if required. | "01" to "12" |
MMM | Abbreviated month name. Date.!CultureInfo.abbreviatedMonthNames. | "Jan" to "Dec" |
MMMM | The full month name. Date.!CultureInfo.monthNames. | "January" to "December" |
yy | Displays the year as a two-digit number. | "99" or "07" |
yyyy | Displays the full four digit year. | "1999" or "2007" |
t | Displays the first character of the A.M./P.M. designator. Date.!CultureInfo.amDesignator or Date.!CultureInfo.pmDesignator | "A" or "P" |
tt | Displays the A.M./P.M. designator. Date.!CultureInfo.amDesignator or Date.!CultureInfo.pmDesignator | "AM" or "PM" |
S | The ordinal suffix ("st, "nd", "rd" or "th") of the current day. | "st, "nd", "rd" or "th" |
Custom Date and Time Format Specifiers
Format | Description | Example |
---|
d | The CultureInfo shortDate Format Pattern | "M/d/yyyy" |
D | The !CultureInfo longDate Format Pattern | "dddd, MMMM dd, yyyy" |
F | The !CultureInfo fullDateTime Format Pattern | "dddd, MMMM dd, yyyy h:mm:ss tt" |
m | The !CultureInfo monthDay Format Pattern | "MMMM dd" |
r | The !CultureInfo rfc1123 Format Pattern | "ddd, dd MMM yyyy HH:mm:ss GMT" |
s | The !CultureInfo sortableDateTime Format Pattern | "yyyy-MM-ddTHH:mm:ss" |
t | The !CultureInfo shortTime Format Pattern | "h:mm tt" |
T | The !CultureInfo longTime Format Pattern | "h:mm:ss tt" |
u | The !CultureInfo universalSortableDateTime Format Pattern | "yyyy-MM-dd HH:mm:ssZ" |
y | The !CultureInfo yearMonth Format Pattern | "MMMM, yyyy" |
Separator Characters
Character | Name |
---|
/ | forward slash |
| space |
. | period |
- | hyphen |
, | comma |
new Date().toString()
new Date().toString('M/d/yyyy')
Date.today().toString('d-MMM-yyyy')
new Date().toString('HH:mm')
Date.today().toString('MMMM dS, yyyy')
Date.today().toShortDateString()
Date.today().toLongDateString()
new Date().toShortTimeString()
new Date().toLongTimeString()
Core
Date.today().set({ day: 15 })
Date.today().set({ year: 2007, month: 1, day: 20 })
Date.today().add({ days: 2 })
Date.today().add({ years: -1, months: 6, hours: 3 })
Date.today().addYears(1)
Date.today().addMonths(-2)
Date.today().addWeeks(1)
Date.today().addDays(4)
Date.today().addHours(6)
Date.today().addMinutes(-30)
Date.today().addSeconds(15)
Date.today().addMilliseconds(200)
Date.today().moveToFirstDayOfMonth()
Date.today().moveToLastDayOfMonth()
new Date().clearTime()
Date.today().setTimeToNow()
ISO 8601
Date.parse('\"1997-07-16T19:20:15\"')
new Date().toISOString()
Date.today().getISOWeek()
Misc
Date.getMonthNumberFromName('March')
Date.getDayNumberFromName('sat')
Date.isLeapYear(2008)
Date.getDaysInMonth(2007, 9)
Date.today().getWeek()
Date.today().setWeek(1)
var test = new Date();
test.getElapsed()
Date.today().isDaylightSavingTime()
Date.today().hasDaylightSavingTime()