Basic use of dayjs time processing library

Posted by bond00 on Wed, 03 Jun 2020 18:23:55 +0200

Day.js Is a lightweight JavaScript time and date processing library, and Moment.js The API design of is consistent.

This article only introduces some common operations. For details of internationalization, plug-ins, customization and other advanced contents, please refer to the official documents.

Its main characteristics are as follows:

  • And Moment.js Same API and usage
  • Immutable data
  • Support chain operation
  • Only 2kb in size (some advanced uses exist in the form of extension and can be loaded on demand)
  • Full browser compatible
  • Strong international support (I18n)

Official website: https://day.js.org/en

Github: https://github.com/iamkun/dayjs

<!-- more -->

1. Installation

1.1. Download

Download address: https://github.com/iamkun/dayjs/releases

<script src="path/to/dayjs/dayjs.min.js"></script>
<script>
  dayjs().format()
</script>

1.2. CDN

<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
<script>dayjs().format()</script>

1.3. Node.JS

npm install dayjs --save
var dayjs = require('dayjs')
//import dayjs from 'dayjs' // ES 2015

dayjs().format()

1.4. TypeScript

npm install dayjs --save
import * as dayjs from 'dayjs'
dayjs().format()

See official documents for more details: https://day.js.org/docs/en/installation/typescript

2. API

2.1. Analysis

Day.js Does not change or override Javascript native Date.prototype Instead, it creates a new Day.js Object, which can be called directly using dayjs().

Day.js Object is immutable, all API operations will return a new Day.js Object.

// Returns the Day.js  object
// No transfer, equivalent to dayjs(new Date())
let now = dayjs();

// Passing in a standard ISO 8601 time string
// https://en.wikipedia.org/wiki/ISO_8601
let date = dayjs('2020-06-01');

// Pass in a Unix timestamp (13 bits)
let date = dayjs(1591149248030);

// Passing in a Javascript Date object
let date = dayjs(new Date(2020, 6, 1));

// Because Day.js  The object is immutable. You can obtain a copy of the object by using the following methods
let date1 = date.clone(); // Method 1: in a Day.js  Use clone function on object
let date2 = dayjs(date); // Method 2: pass in a Day.js  object

// Check current Day.js  Whether the object is a valid date time
if (dayjs().isValid()) {
  // Effective
} else {
  // invalid
}

2.2. Obtaining and setting

// Get, returns a value of type number
dayjs().year(); // Year = = > dayjs(). Get ('year ')
dayjs().month(); // Month = = > dayjs(). Get ('month ')
dayjs().date(); // Day = = > dayjs(). Get ('date ')
dayjs().hour(); // Time = = > dayjs(). Get ('hour ')
dayjs().minute(); // Score = = > dayjs(). Get ('minute ')
dayjs().second(); // Seconds = = > dayjs(). Get ('second ')
dayjs().millisecond(); // Ms = = > dayjs(). Get ('millisecond ')
dayjs().day(); // Day of the week = = > dayjs(). Get ('day ')

// Setting, the value case corresponding to the unit is not sensitive
dayjs().set('month', 3);
dayjs().set('second', 30);

2.3. Operation

// increase
dayjs().add(7, 'day'); // Add 7 days

// reduce
dayjs().subtract(2, 'month'); // Decrease by 2 months

//  start
dayjs().startOf('month'); // First day of the month

// end
dayjs().endOf('year'); // Last day of the year

2.4. Display

// format
dayjs().format(); // Default format, such as: 2020-06-03T20:06:13+08:00
dayjs().format("YYYY-MM-DD HH:mm:ss"); // Specified format 2020-06-03 20:07:12

// Get two Day.js  Time difference of object, default MS, unit can be specified
dayjs('2020').diff(dayjs('1998')); // 694224000000
dayjs('2020').diff(dayjs('1998'), 'year'); // 22

// time stamp
dayjs().valueOf(); // millisecond
dayjs().unix(); // second

// Days
dayjs('2020-07').daysInMonth(); // 31

// Native Date object
dayjs().toDate(); // Wed Jun 03 2020 20:13:40 GMT+0800 (China Standard Time)

// Returns a string in ISO 8601 format
dayjs().toJSON(); // "2020-06-03T12:15:54.635Z"
dayjs().toISOString(); // "2020-06-03T12:16:48.199Z"

2.5. Query

// Check if one date is before another
dayjs('2020-06-03').isBefore(dayjs('2020-05-03')); // false

// Check if one date is after another
dayjs('2020-06-03').isAfter(dayjs('2020-05-03')); // true

// Check if the two dates are the same
dayjs('2020-06-03').isSame(dayjs('2020-06-03')); // true

Topics: Front-end Javascript github npm TypeScript