Date Wrapper
A simple library to wrap js dates with extra functionality, easy validation, immutability, chainability, and string formatting
Usage
Available as npm package including cjs, esm, umd, and iife
npm install date-wrapper
In browser using script tags
date-wrapper.iife.js
date-wrapper.iife.min.js
Features and Examples
Create an object using strings or standard js Dates
dateWrapper(dateInput, is_ddmmFormat?) => dateWrapper Object
const D = dateWrapper;
D(new Date());
D('2019/01/01');
D('2019/1/1');
D('01/01/2019');
D('1/1/2019');
D('31/12/2019', true);
D('1/1/2019', true);
D('01-01-2019');
Invalid dates will return undefined, making for easy string validation
D('13/01/2019');
D('01/32/2019');
D('02/29/2019');
D('02/29/2020');
Setting the time
.setTime(hours, minutes, seconds, milliseconds) => dateWrapper object
D('01/01/2019');
D('01/01/2019').setTime(8, 30, 59, 999);
D('01/01/2019').setTime(8);
Adding to or Subtracting from the date
.addDays(days) => dateWrapper object
.addYears(years) => dateWrapper object
D('01/01/2019').addDays(9);
D('01/01/2019').addDays(-1);
D('01/01/2019').addYears(1);
D('01/01/2019').addYears(-19);
Adding to or Subtracting from the time
.addTime(hours, minutes, seconds, milliseconds) => dateWrapper object
D('01/01/2019').addTime(25, 10, -20, 0);
D('01/01/2019').addTime(0, -1);
Immutability and chaining
var today = D('01/01/2019');
var dt = today.addDays(32).addYears(1);
var tomorrow = today.addDays(1);
today;
var jsDate = today.toDate();
jsDate.setDate(jsDate.getDate() + 10);
today;
Comparing dates with or without the time component
.equals(dateOrDateWrapper, compareIncludingTime?) => boolean
.isOnOrBefore(dateOrDateWrapper, compareIncludingTime?) => boolean
.isBefore(dateOrDateWrapper, compareIncludingTime?) => boolean
.isOnOrAfter(dateOrDateWrapper, compareIncludingTime?) => boolean
.isAfter(dateOrDateWrapper, compareIncludingTime?) => boolean
var isSame = D(inputStr).equals(D('1/1/2019'));
var isSame = D(inputStr).equals(new Date(2019, 0, 1));
var D_now = D(new Date());
var D_userInput = D(getUserInput());
D_userInput.equals(D_now);
D_userInput.equals(D_now, true);
D_userInput.isOnOrAfter(D_now);
D_userInput.isOnOrAfter(D_now, true);
D_userInput.isOnOrBefore(D_now);
D_userInput.isOnOrBefore(D_now, true);
D_userInput.isAfter(D_now);
D_userInput.isAfter(D_now, true);
D_userInput.isBefore(D_now);
D_userInput.isBefore(D_now, true);
Timespan between two dates
.from(dateOrDateWrapper) => {
days: number,
hours: number,
minutes: number,
seconds: number,
ms: number
}
var timespan = D(tomorrow).from(D(today));
var timespan = D(today).from(D(tomorrow));
Getting first and last days of the current month or year
.firstOfMonth() => DateWrapper object
.lastOfMonth() => DateWrapper object
.firstOfYear() => DateWrapper object
.lastOfYear() => DateWrapper object
D('05/15/2019').firstOfMonth();
D('05/15/2019').lastOfMonth();
D('05/15/2019').firstOfYear();
D('05/15/2019').lastOfYear();
String and js Date representation
.toDate() => Date
.toString() => string
Custom formatting using toString()
parameter
.toString(formatString) => string
code | example | | code | example | | code | example | | code | example |
---|
yyyy | 2019 | | M | 1 | | d | 1 | | H | 16 |
yy | 19 | | MM | 01 | | dd | 01 | | HH | 16 |
| | | MMM | Jan | | ddd | Tue | | h | 4 |
| | | MMMM | January | | dddd | Tuesday | | hh | 04 |
| | | | | | | | | mm | 29 |
| | | | | | | | | ss | 59 |
| | | | | | | | | fff | 050 |
| | | | | | | | | tt | PM |
var date = D('01/01/2019').setTime(16, 29, 59, 50);
date.toString('MM/dd/yy');
date.toString('dddd MMMM d, yyyy hh:mm:ss.fff tt');
var timestamp = date.toString('yyyyMMdd-HHmmss');