When using vue to develop projects, the front end often needs to calculate some date and time, such as:
Recommend a lightweight JavaScript library for processing time and date: dayjs It is very convenient to use this plug-in to calculate common dates 1. Install dayjs in the project, the command is: 2. In main.js , add the following two lines of code import dayjs from 'dayjs'; Vue.prototype.dayjs = dayjs; 3. Use it directly where needed on the page The current date or timestamp is converted . The format is followed by the content you want to format. **dayjs()** does not put any parameters in the brackets. The default is the current date. You can also put a timestamp to convert directly. (Note: When using the date picker of the Element component, its value-format attribute requires: |
Components | Dayjs (format) | Element(value-format) |
---|---|---|
Year | YYYY | yyyy |
moon | MM | MM |
day | DD | dd |
hour | HH | HH |
point | mm | mm |
Second | ss | ss |
The expressions of year and day are slightly different and easy to confuse.)
this.dayjs().format("YYYY-MM-DD") this.dayjs().format("YYYY-MM-DD HH:mm") this.dayjs(1614787200000).format("YYYY-MM-DD HH:mm:ss")
2. Calculate the start and end dates of a week/month/year. The methods used are startOf() and endOf() . The brackets can be "week", "month", or "year".
(ps: adding format is for more intuitiveness)
this.dayjs().startOf("week"); this.dayjs().endOf("week").format("YYYY-MM-DD"); this.dayjs().startOf("month").format("YYYY-MM-DD"); this.dayjs("2021-02-09").endOf("month").format("YYYY-MM-DD");
Calculate dates, such as a few days ago or a few days later.
Front (minus) | After (add) |
---|---|
subtract(param1, parameter2) | add(param1, parameter2) |
Parameter 1 | number |
Parameter 2 | Unit ("week", "month", "year") |
this.dayjs().subtract(3,'day').format("YYYY-MM-DD") this.dayjs().subtract(3,'month').format("YYYY-MM-DD") this.dayjs().add(12,'day').format("YYYY-MM-DD") this.dayjs('2021-03-12').add(45,'day').format("YYYY-MM-DD")
5. Get the day of the month, use the method dayInMonth()
this.dayjs().daysInMonth(); this.dayjs("2021-02-09").daysInMonth(); this.dayjs("2021-01").daysInMonth();
6. Convert ordinary date to timestamp
this.dayjs().unix() this.dayjs('2020-10-04').unix()
Summarize
This is the end of this article about how Vue uses Dayjs to calculate common dates. For more information about Vue calculating common dates, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!
<<: Linux/Mac MySQL forgotten password command line method to change the password
>>: Summary of Ubuntu backup methods (four types)
MySQL's CAST() and CONVERT() functions can be...
When dynamically concatenating strings, we often ...
Often, after a web design is completed, the desig...
<br />For an article on a content page, if t...
Table of contents 1. The concept of filter 1. Cus...
A colleague asked for help: the login to the back...
Recently, when I was using Linux to log in locall...
Preface If we want to achieve the effect of onlin...
To execute a shell command in Docker, you need to...
Preface In the previous article, we mainly learne...
1. What is Continuous Delivery The software produ...
Generate Linux library The Linux version uses cen...
Preface The requirement implemented in this artic...
I'm currently working on my own small program...
Table of contents Creating OAuth Apps Get the cod...