- Stepping
beginningOfDay,endOfWeek, etc.- Utility functions
isLeapYear,daysInMonth,weeksInMonth, etc.- Variable first day of week
- The first day of week for above calculations is customizable, defaults to Sunday.
Download
- time.js v1.0
- time.js (minified) v1.0
Documentation
Usage
Creating instances
var t = new Time();
var t = new Time(2008); // January 1st 2008, 00:00:00
var t = new Time(2008, 5, 25); // May 25th 2008, 00:00:00
var t = new Time(2008, 5, 25, 17, 30, 25); // May 25th 2008, 17:30:25
Setting firstDayOfWeek
firstDayOfWeek defaults to 0, Sunday. Set it to any value between 0 and 6. 6 is Saturday. It's settable globally and per instance.
Time.firstDayOfWeek = 1; // Monday
new Time(2008, 11, 2).firstDayOfWeek = 1;
Getting and setting attributes
var t = new Time();
t.hour(); // Get
t.hour(17); // Set (minute, second etc. is left untouched)
These are the available attributes. They are all gettable and settable.
year
month
hour
minute
second
millisecond
epoch // (UNIX time in milliseconds)
weekday // (1-7)
Stepping
These are the available stepping functions.
beginningOfYear endOfYear
beginningOfMonth endOfMonth
beginningOfDay endOfDay
beginningOfHour endOfHour
beginningOfMinute endOfMinute
beginningOfWeek endOfWeek
previousMonth nextMonth
advanceMonths
advanceDays
firstDayInCalendarMonth
Examples of usage.
new Time(2008, 5, 17).beginningOfYear(); // January 1st 2008, 00:00
new Time(2008, 5, 17, 23, 52).beginningOfDay(); // May 5th 2008, 00:00
new Time(2008, 5, 17, 16, 30).endOfHour(); // May 5th 2008, 16:59
new Time(2008, 5, 17).endOfYear(); // December 31th 2008, 23:59
new Time(2008, 1).previousMonth(); // December 1st 2007, 00:00
new Time(2007, 1, 31).advanceMonths(1); // February 28th 2007
new Time(2008, 1, 31).advanceMonths(1); // February 29th 2008
new Time(2008, 1, 3).advanceMonths(-13); // December 3rd 2006
new Time(2008, 5, 17, 16, 30).advanceDays(1); // May 18th 2008, 16:30
new Time(2008, 5, 17).firstDayInCalendarMonth(); // April 27th 2008, 00:00:00
Utility
clone
Creates a copy of the time instance.
var time = new Time(2006);
var timeClone = time.clone().setYear(1999)
time.year() // 2006
timeClone.year() // 1999
daysInMonth
new Time(2008, 1).daysInMonth(); // 31
new Time(2007, 2).daysInMonth(); // 28
new Time(2007, 4).daysInMonth(); // 30
isLeapYear
new Time(1804).isLeapYear(); // true
weeksInMonth
new Time(2008, 2).weeksInMonth() // 5
new Time(2008, 3).weeksInMonth() // 6
weekOfCurrentMonth
The row in a typical month calendar.
new Time(2008, 11, 1).weekOfCurrentMonth() // 1
new Time(2008, 11, 2).weekOfCurrentMonth() // 2