Better times is a small .js file that makes dates (in JavaScript) fun again.
- Goal
- Functions
- Date.prototype.add
- Date.prototype.subtract
- Date.prototype.nextDay
- Date.prototype.nextWeek
- Date.prototype.nextYear
- Date.prototype.prevDay
- Date.prototype.prevWeek
- Date.prototype.prevYear
- Date.prototype.endOfDay
- Date.prototype.endOfMonth
- Date.prototype.endOfYear
- Date.prototype.startOfDay
- Date.prototype.startOfMonth
- Date.prototype.startOfYear
- Time unit
- Contribute
The goal of Better Times is to make date manipulation in JavaScript easier and safer.
The following functions are added to the Date.prototype
Add to the date the relevant time unit.
let date = new Date('2010-01-01T00:00:00.000Z');
date.add('years', 1); // change date to: 2011-01-01T00:00:00.000Z
Subtract to the date the relevant time unit.
let date = new Date('2011-01-01T00:00:00.000Z');
date.subtract('years', 1); // change date to: 2010-01-01T00:00:00.000Z
Change date to the next day
let date = new Date('2010-01-01T00:00:00.000Z');
date.nextDay(); // change date to: 2010-01-02T00:00:00.000Z
Change date to the next week
let date = new Date('2010-01-01T00:00:00.000Z');
date.nextWeek(); // change date to: 2010-01-08T00:00:00.000Z
Change date to the next month
let date = new Date('2010-01-01T00:00:00.000Z');
date.nextMonth(); // change date to: 2010-02-01T00:00:00.000Z
Change date to the next year
let date = new Date('2010-01-01T00:00:00.000Z');
date.nextYear(); // change date to: 2011-01-01T00:00:00.000Z
Change date to the previous day
let date = new Date('2010-01-02T00:00:00.000Z');
date.prevDay(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the previous week
let date = new Date('2010-01-08T00:00:00.000Z');
date.prevWeek(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the previous month
let date = new Date('2010-02-01T00:00:00.000Z');
date.prevMonth(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the previous year
let date = new Date('2010-02-01T00:00:00.000Z');
date.prevYear(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the end of the day.
let date = new Date('2010-01-01T00:00:00.000Z');
date.endOfDay(); // change date to: 2010-01-01T23:59:59.000Z
Change date to the end of the month and day.
let date = new Date('2010-01-01T00:00:00.000Z');
date.endOfMonth(); // change date to: 2010-01-31T23:59:59.000Z
Change date to the end of the year and day.
let date = new Date('2010-01-01T00:00:00.000Z');
date.endOfYear(); // change date to: 2010-12-31T23:59:59.000Z
Change date to the start of the day.
let date = new Date('2010-01-01T23:59:59.000Z');
date.startOfDay(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the start of the month and the day.
let date = new Date('2010-01-31T23:59:59.000Z');
date.startOfMonth(); // change date to: 2010-01-01T00:00:00.000Z
Change date to the start of the year and the day.
let date = new Date('2010-12-31T23:59:59.000Z');
date.startOfYear(); // change date to: 2010-01-01T00:00:00.000Z
Note: All time units should end with an 's', if not, the 's' is added automatically
- seconds
- minutes
- hours
- days
- weeks
- months
- years
PRs and issues are more then welcome.