Dayjs is a lightweight JavaScript library that handles times and dates

The official making github.com/iamkun/dayj…

Chinese usage document github.com/iamkun/dayj…

Dayjs benefits

  • 🕒 and moment.js share the same API and schema
  • 💪 immutable and persistent
  • 🔥 provides chained calls
  • 🌐 International standards
  • 📦 ultra-small compression volume, only about 2KB
  • 👫 most browsers are compatible

Dayjs installation

npm install dayjs --save
import dayjs from 'dayjs'
Copy the code
<script src="https://unpkg.com/dayjs"></script>
Copy the code

The return values of day.js are all new Dayjs objects

Dayjs parses the relevant API

Run dayjs() directly to get the dayJS object containing the current time and date

dayjs()  // Return current time Tue, 28 May 2019 05:57:34 GMT
Copy the code

It is possible to parse a standard ISO 8601 time string passed in.

dayjs('1995-12-25') //Sun, 24 Dec 1995 16:00:00 GMT
Copy the code

You can parse a Javascript Date object passed in.

dayjs(new Date(2018, 8, 18)) //Mon, 17 Sep 2018 16:00:00 GMT
Copy the code

You can parse an incoming Unix timestamp (13 digits).

dayjs(1318781876406) //Sun, 16 Oct 2011 16:17:56 GMT
Copy the code

Dayjs objects are immutable. If you want to get a copy of the object, execute.clone(). Passing a dayJS object to dayjs() will do the same.

dayjs(Dayjs)
dayjs().clone()
Copy the code

Checks whether the current Dayjs object is a valid time

dayjs().isValid()
Copy the code

Dayjs gets and sets the relevant API

Gets or sets the year.

dayjs().year() //2019
dayjs().year(2000) //Sun, 28 May 2000 06:14:07 GMT
Copy the code

Gets or sets the month. Starting from 0

Dayjs ().month() //4 actually May dayjs(). Month (0) //Mon, 28 Jan 2019 06:15:25 GMTCopy the code

Gets or sets the date. Starting from 1

Dayjs ().date() //28 Dayjs (). Date (1) // May 2019 06:17:04 GMTCopy the code

Gets or sets the week. It starts at 0 on Sunday

Dayjs ().day() //2 It is Tuesday dayjs().day(0) //Sun, 26 May 2019 06:18:13 GMT is forcibly set to SundayCopy the code

Gets or sets the hour

Hour (12) //Tue, 28 May 2019 04:21:30 GMT UnclearCopy the code

Gets or sets the minutes.

dayjs().minute() //22 
dayjs().minute(59) //Tue, 28 May 2019 06:59:50 GMT
Copy the code

Gets or sets seconds

dayjs().millisecond()
dayjs().millisecond(1)
Copy the code

Gets or sets milliseconds.

dayjs().millisecond()
dayjs().millisecond(1)
Copy the code

The unit passed in to get the information taken from the Dayjs object is case insensitive

dayjs().get('month') //4
Copy the code
unit abbreviations describe
date The date of
day d What day of the week (Sunday 0, Saturday 6)
month M Month (January 0, December 11)
year y years
hour h when
minute m points
second s seconds
millisecond ms ms

Set a time

dayjs().set('date'.1);
dayjs().set('month'.3); / / April
dayjs().set('second'.30);
Copy the code

Apis for dayJS operations

You can add or subtract the Dayjs objects as follows

Increments the time and returns a new Dayjs() object.

dayjs().add(value : Number, unit : String); dayjs().add(7, 'day'); // Add 7 days to the current numberCopy the code

Reduces the time and returns a new Dayjs() object.

dayjs().subtract(value : Number, unit : String); dayjs().subtract(7, 'day'); // Reduce 7 days from the current levelCopy the code

Returns the Dayjs() object for the beginning of the current time, such as the first day of the month.

dayjs().startOf(unit : String);
dayjs().startOf('month'); //Tue, 30 Apr 2019 16:00:00 GMT 
Copy the code

Dayjs() object that returns the end time of the current time, such as the last day of the month.

dayjs().endOf(unit : String);
dayjs().endOf('month'); //Fri, 31 May 2019 15:59:59 GMT
Copy the code

Display relevant apis

Format the Dayjs object and display it.

Receives a series of time and date strings and replaces them with the corresponding values.

dayjs().format(String)
dayjs('2019-01-25').format('YYYY-MM-DD HH:mm:ss') 
dayjs().format('YYYY-MM-DD') 
dayjs().format('YYYY-MM') //2019
Copy the code

Gets the time difference between two Dayjs objects, milliseconds by default.

const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000
date1.diff(date2, 'month') // 7
date1.diff(date2, 'month', true) // 7.645161290322581
date1.diff(date2, 'day') // 233
Copy the code

Return Unix timestamp (ms)

dayjs().valueOf()
Copy the code

Returns the Unix timestamp in seconds.

dayjs().unix()
Copy the code

Returns the number of days in the month

dayjs().daysInMonth()
Copy the code

Returns a native Date object.

dayjs().toDate()
Copy the code

When serializing a Dayjs object, a string in ISO8601 format is returned.

Dayjs (). The toJSON () / / "the 2018-08-08 T00:00:00. 000 z"Copy the code

The value is a string in ISO8601 format.

dayjs().toISOString()
Copy the code

Return string

dayjs().toString()
Copy the code

Query the relevant API to check whether one Dayjs object precedes another Dayjs object.

dayjs().isBefore(Dayjs, unit? : String);
dayjs().isBefore(dayjs()); // false
dayjs().isBefore(dayjs(), 'year'); // false
Copy the code

Check if one Dayjs object is at the same time as another Dayjs object.

dayjs().isSame(Dayjs, unit? : String);
dayjs().isSame(dayjs()); // true
dayjs().isSame(dayjs(), 'year'); // true
Copy the code

Check if one Dayjs object is after another Dayjs object.

dayjs().isAfter(Dayjs, unit? : String);
dayjs().isAfter(dayjs()); // false
dayjs().isAfter(dayjs(), 'year'); // false
Copy the code