xx45 / dayjs
- среда, 25 апреля 2018 г. в 00:18:05
JavaScript
⏰ Fast 2KB date library alternative to Moment.js with the same modern API
English | 简体中文
Fast 2kB alternative to Moment.js with the same modern API
Day.js is a minimalist JavaScript library for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js.
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');You have multiple ways of getting Day.js:
npm install dayjs --save var dayjs = require('dayjs');
dayjs().format(); <!-- Latest compiled and minified JavaScript -->
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>Just download the latest version of Day.js at https://unpkg.com/dayjs
Instead of modifying the native Date.prototype, Day.js creates a wrapper for the Date object, called Dayjs object.
Dayjs object is inmmutable, that is to say, all api operation will return a new Dayjs object.
Api will always return a new Dayjs object if not specified.
Simply call dayjs() with one of the supported input types.
To get the current date and time, just call dayjs() with no parameters.
dayjs();Creating from a string matches ISO 8601 format.
dayjs(String);
dayjs("1995-12-25");Passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC).
dayjs(Number);
dayjs(1318781876406);Passing a pre-existing native Javascript Date object.
dayjs(Date);
dayjs(new Date(2018, 8, 18));All Dayjs are inmmutable. If you want a copy of the object, just call .clone().
Calling dayjs() on a Dayjs object will also clone it.
dayjs(Dayjs);
dayjs().clone();Check whether the Dayjs object considers the date invalid.
dayjs().isValid();Get and set date.
Get year.
dayjs().year();Get month.
dayjs().month();Get day of the month.
dayjs().date();Get hour.
dayjs().hour();Get minute.
dayjs().minute();Get second.
dayjs().second();Get millisecond.
dayjs().millisecond();Date setter. Units are case insensitive
dayjs().set(unit : String, value : Int);
dayjs().set('month', 3); // April
moment().set('second', 30);Once you have a Dayjs object, you may want to manipulate it in some way like this:
dayjs().startOf('month').add(1, 'day').subtract(1, 'year')Return a new Dayjs object by adding time.
dayjs().add(value : Number, unit : String);
dayjs().add(7, 'day');Return a new Dayjs object by subtracting time. exactly the same as dayjs#add.
dayjs().subtract(value : Number, unit : String);
dayjs().subtract(7, 'year');Return a new Dayjs object by by setting it to the start of a unit of time.
dayjs().startOf(unit : String);
dayjs().startOf('year');Return a new Dayjs object by by setting it to the end of a unit of time.
dayjs().endOf(unit : String);
dayjs().endOf('month');Once parsing and manipulation are done, you need some way to display the Dayjs object.
Takes a string of tokens and replaces them with their corresponding date values.
dayjs().format(String);
dayjs().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
dayjs().format("[YYYY] MM-DDTHH:mm:ssZ"); // "[2014] 09-08T08:02:17-05:00"Get the difference of two Dayjs object in milliseconds or other unit.
dayjs().diff(Dayjs, unit);
dayjs().diff(dayjs(), 'years'); // 0Outputs the number of milliseconds since the Unix Epoch
dayjs().valueOf();Outputs a Unix timestamp (the number of seconds since the Unix Epoch).
dayjs().unix();Get the number of days in the current month.
dayjs().daysInMonth();Date objectGet copy of the native Date object from Dayjs object.
dayjs().toDate();Return an array that mirrors the parameters from new Date().
dayjs().toArray(); //[2018, 8, 18, 00, 00, 00, 000];Serializing an Dayjs to JSON, will return an ISO8601 string.
dayjs().toJSON(); //"2018-08-08T00:00:00.000Z"Formats a string to the ISO8601 standard.
dayjs().toISOString();Return an object with year, month ... missisecond.
dayjs().toObject();// { years:2018, months:8, date:18, hours:0, minutes:0, seconds:0, milliseconds:0}dayjs().toString(); Check if a Dayjs object is before another Dayjs object.
dayjs().isBefore(Dayjs);
dayjs().isBefore(dayjs()); // falseCheck if a Dayjs object is same as another Dayjs object.
dayjs().isSame(Dayjs);
dayjs().isSame(dayjs()); // trueCheck if a Dayjs object is after another Dayjs object.
dayjs().isAfter(Dayjs);
dayjs().isAfter(dayjs()); // falseCheck if a year is a leap year.
dayjs().isLeapYear();
dayjs('2000-01-01').isLeapYear(dayjs()); // trueMIT