Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article introduces the use of day.js using real business scenarios, which will be updated in the future, and you are welcome to leave your real business scenarios in the comments, where the answers will be provided.

Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers. And at just 2KB in size, it’s much lighter than moment.js.

Run NPM install dayjs –save to install day.js in Vue and React. Import dayjs from ‘dayjs’.

Parse and format millisecond timestamps

Format: 2021-9-24 21:39:23

dayjs(1632490763000).format('YYYY-MM-DD HH:mm:ss')
Copy the code

Parses and formats the second-level timestamp

Format: 2021-9-24 21:39:23

dayjs.unix(1632490763).format('YYYY-MM-DD HH:mm:ss')
Copy the code

Get the time stamps 00:00:00 and 23:59:59 of the current day

  • On the day of 00:00:00
Dayjs (). The startOf (" date "). The valueOf () / / millisecond time stamp dayjs (). The startOf (" date "). The Unix () / / second level timestampCopy the code
  • On the day of 23:59:59
Dayjs (). The endOf (" date "). The valueOf () / / millisecond time stamp dayjs (). The endOf (" date "). The Unix () / / second level timestampCopy the code

Get the timestamp for tomorrow

Dayjs (). The add (1, 'day'). The valueOf () / / millisecond time stamp dayjs (). The add (1, 'day), Unix () / / second level timestampCopy the code

Get yesterday’s timestamp

Dayjs (). The add (1, 'day'). The valueOf () / / millisecond time stamp dayjs (). The add (1, 'day'). Unix timestamp () / / second levelCopy the code

Obtain the millisecond timestamp of the current time 00:00:00 7 days ago and 23:59:59 7 days later

  • The current time is a millisecond timestamp of 00:00:00 seven days ago
dayjs().subtract(7, 'day').startOf('date').valueOf()
Copy the code
  • The current time is the millisecond timestamp of 23:59:59 in 7 days
dayjs().add(7, 'day').endOf('date').valueOf()
Copy the code

Gets the second time stamps of 00:00:00 on the first day and 23:59:59 on the last day of the month

  • The second time stamp of 00:00:00, the first day of the current month
dayjs().startOf('month').unix()
Copy the code
  • The current time is the second – level timestamp of 23:59:59 on the last day of the month
dayjs().endOf('month').unix()
Copy the code

Retrieves the second time stamps of 00:00:00 on 24th of last month and 23:59:59 on 24th of next month

  • A second time stamp at 00:00:00 on the 24th of last month
dayjs().subtract(1, 'month').date(24).startOf('date').unix()
Copy the code
  • 23:59:59 second time stamp on the 24th of next month
dayjs().subtract(1, 'month').date(24).endOf('date').unix()
Copy the code

Gets the millisecond time stamps for last Monday 00:00:00 and next Monday 23:59:59

  • Last Monday 00:00:00 millisecond time stamp
dayjs().subtract(1, 'week').day(1).startOf('date').valueOf()
Copy the code
  • Next Monday 23:59:59 millisecond time stamp
dayjs().add(1, 'week').day(1).endOf('date').valueOf()
Copy the code

Retrieves the second time stamps for December 24 00:00:00 last year and December 24 00:00:00 next year

  • A second time stamp at 00:00:00 on December 24 last year
dayjs().subtract(1, 'year').month(11).date(24).startOf('date').unix()
Copy the code
  • Second time stamp at 00:00:00, December 24, next year
dayjs().add(1, 'year').month(11).date(24).startOf('date').unix()
Copy the code

Determine whether the current time is before or after September 25, 2021, or right now

const time = dayjs('2021-09-25'); If (dayjs().isbefore (time,'date')){// before 25 September 2021}else if(dayjs().isafter (time,'date')){// after 25 September 2021}else If (dayjs().issame (time,'date')){// On September 25, 2021 today}Copy the code

Determine if the current time is between September 20, 2021 and September 25, 2021

const time1 = dayjs('2021-09-20'); const time2 = dayjs('2021-09-25'); If (dayjs().isafter (time2,'date') &&dayjs ().isbefore (time2,'date')){// Between 20 September 2021 and 25 September 2021}Copy the code

Or introduce the isBetween plug-in of day.js to achieve this

import dayjs from 'dayjs'; import isBetween from 'dayjs/plugin/isBetween'; dayjs.extend(isBetween); const time1 = dayjs('2021-09-20'); const time2 = dayjs('2021-09-25'); If (dayjs().isbetween (time1, time2, 'date')){// Between 20 September 2021 and 25 September 2021}Copy the code

Format a second time stamp for xx month XX XXXX is Xiaoming’s birthday

Unix (1632490763). Format ('YYYY-MM-DD HH: MM: SS is Xiao Ming's birthday ')Copy the code

subsequent

Feel free to leave your actual business scenarios in the comments, and the answers will be provided here.