当前位置:网站首页>JS common time operation moment JS reference document
JS common time operation moment JS reference document
2022-06-10 08:18:00 【Back end regular developer】
Moment.js It's a lightweight JavaScript Time bank , It facilitates the operation of time in daily development , Improved development efficiency . Daily development , The following operations are usually performed on the time : Such as getting time , Setup time , Format time , Compare time and so on . Here is my answer to moment.js Sorting during use , For future reference .
One 、 introduce moment.js
1.Node.js Mode introduction
(1) install
npm install moment perhaps yarn add moment
(2) introduce
// require The way
var moment = require('moment');
// import The way
import moment from 'moment';
2. Browser mode to introduce
<script src="moment.js"></script>
Two 、moment Time zones and transitions
1. Time zone settings
// require The way
require('moment/locale/zh-cn')
moment.locale('zh-cn');
// import The way
import 'moment/locale/zh-cn'
moment.locale('zh-cn');
// Use utc The time zone , You can continue to add related acquisition time or formatting operations later
moment.utc()
2.UTC And Beijing time
2.1 The goal is UTC Time , Then use moment(utcTime).utc() Method , give an example :
moment(utcTime).utc().format('YYYY-MM-DD HH:mm:ss')
2.2 The target is Beijing time , Then use moment(utcTime).utcOffset(8) Method , give an example :
moment(utcTime).utcOffset(8).format('YYYY/MM/DD HH:mm:ss')
3、 ... and 、 Use
1. Acquisition time
(1) Get the current time
moment()
(2) Get today 0 when 0 branch 0 second
moment().startOf('day')
(3) Get the first day of the week ( Sunday )0 when 0 branch 0 second
moment().startOf('week')
(4) Get this Monday 0 when 0 branch 0 second
moment().startOf('isoWeek')
(5) Get the first day of the current month 0 when 0 branch 0 second
moment().startOf('month')
(6) Get today 23 when 59 branch 59 second
moment().endOf('day')
(7) Get the last day of the week ( Saturday )23 when 59 branch 59 second
moment().endOf('week')
(8) Get this Sunday 23 when 59 branch 59 second
moment().endOf('isoWeek')
(9) Get the last day of the current month 23 when 59 branch 59 second
moment().endOf('month')
(10) Gets the total number of days in the current month
moment().daysInMonth()
(11) Get the timestamp ( In seconds )
moment().format('X') // The return value is of string type
moment().unix() // The return value is numeric
(12) Get the timestamp ( In Milliseconds )
moment().format('x') // The return value is of string type
moment().valueOf() // The return value is numeric
(13) Get year
moment().year()
moment().get('year')
(14) Get month
moment().month() // (0~11, 0: January, 11: December)
moment().get('month')
(15) Get a day of the month
moment().date()
moment().get('date')
(16) Get a day of the week
moment().day() // (0~6, 0: Sunday, 6: Saturday)
moment().weekday() // (0~6, 0: Sunday, 6: Saturday)
moment().isoWeekday() // (1~7, 1: Monday, 7: Sunday)
moment().get('day')
mment().get('weekday')
moment().get('isoWeekday')
(17) For hours
moment().hours()
moment().get('hours')
(18) Get minutes
moment().minutes()
moment().get('minutes')
(19) Get seconds
moment().seconds()
moment().get('seconds')
(20) Gets the current year, month, day, minute, second
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
moment().toObject() // {years: xxxx, months: x, date: xx ...}
2. Setup time
(1) Set year
moment().year(2019)
moment().set('year', 2019)
moment().set({
year: 2019})
(2) Set month
moment().month(11) // (0~11, 0: January, 11: December)
moment().set('month', 11)
(3) Set a day of the month
moment().date(15)
moment().set('date', 15)
(4) Set a day of the week
moment().weekday(0) // Set the date to the first day of the week ( Sunday )
moment().isoWeekday(1) // Set the date to Monday of this week
moment().set('weekday', 0)
moment().set('isoWeekday', 1)
(5) Set hours
moment().hours(12)
moment().set('hours', 12)
(6) Set minutes
moment().minutes(30)
moment().set('minutes', 30)
(7) Set seconds
moment().seconds(30)
moment().set('seconds', 30)
(8) year +1
moment().add(1, 'years')
moment().add({
years: 1})
(9) month +1
moment().add(1, 'months')
(10) date +1
moment().add(1, 'days')
(11) week +1
moment().add(1, 'weeks')
(12) Hours +1
moment().add(1, 'hours')
(13) minute +1
moment().add(1, 'minutes')
(14) Number of seconds +1
moment().add(1, 'seconds')
(15) year -1
moment().subtract(1, 'years')
moment().subtract({
years: 1})
(16) month -1
moment().subtract(1, 'months')
(17) date -1
moment().subtract(1, 'days')
(18) week -1
moment().subtract(1, 'weeks')
(19) Hours -1
moment().subtract(1, 'hours')
(20) minute -1
moment().subtract(1, 'minutes')
(21) Number of seconds -1
moment().subtract(1, 'seconds')
3. Format time
| Format code | explain | Return value example |
|---|---|---|
| M | The number of months , No leading zeros | 1 To 12 |
| MM | The number of months , There are leading zeros | 01 To 12 |
| MMM | Three letter abbreviations for the month | Jan To Dec |
| MMMM | month , Full text format | January To December |
| Q | quarter | 1 To 4 |
| D | The day of the month , No leading zeros | 1 To 31 |
| DD | The day of the month , There are leading zeros | 01 To 31 |
| d | Day of the week , Digital representation | 0 To 6,0 Means Sunday ,6 It means Saturday |
| ddd | Three letters indicate the day of the week | Sun To Sat |
| dddd | What day , Full text of the week | from Sunday To Saturday |
| w | The week of the year | Such as 42: It means the first one 42 Zhou |
| YYYY | Four digits indicate the full year | Such as :2014 or 2000 |
| YY | Two digit year | Such as :14 or 98 |
| A | uppercase AM PM | AM PM |
| a | Lowercase am pm | am pm |
| HH | Hours ,24 hourly , There are leading zeros | 00 To 23 |
| H | Hours ,24 hourly , No leading zeros | 0 To 23 |
| hh | Hours ,12 hourly , There are leading zeros | 00 To 12 |
| h | Hours ,12 hourly , No leading zeros | 0 To 12 |
| m | Minutes without leading zeros | 0 To 59 |
| mm | Minutes with leading zeros | 00 To 59 |
| s | Seconds without leading zeros | 1 To 59 |
| ss | Description with leading zeros | 01 To 59 |
| X | Unix Time stamp | 1411572969 |
(1) Format the date of year : ‘xxxx year xx month xx Japan ’
moment().format('YYYY year MM month DD Japan ')
(2) Format the date of year : ‘xxxx-xx-xx’
moment().format('YYYY-MM-DD')
(3) Format in minutes and seconds (24 hourly ): ‘xx when xx branch xx second ’
moment().format('HH when mm branch ss second ')
(4) Format in minutes and seconds (12 hourly ):‘xx:xx:xx am/pm’
moment().format('hh:mm:ss a')
(5) Format timestamps ( In Milliseconds )
moment().format('x') // The return value is of string type
4. Compare time
(1) Get the time difference between two dates
let start_date = moment().subtract(1, 'weeks')
let end_date = moment()
end_date.diff(start_date) // Returns the number of milliseconds
end_date.diff(start_date, 'months') // 0
end_date.diff(start_date, 'weeks') // 1
end_date.diff(start_date, 'days') // 7
start_date.diff(end_date, 'days') // -7
5. Turn into JavaScript Native Date object
moment().toDate()
new Date(moment())
6. Date formatting
moment().format('MMMM Do YYYY, h:mm:ss a'); // May 24 Japan 2019, 7:47:43 evening
moment().format('dddd'); // Friday
moment().format("MMM Do YY"); // 5 month 24 Japan 19
moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
moment().format(); // 2019-05-24T19:47:43+08:00
7. Relative time output instance
moment("20111031", "YYYYMMDD").fromNow(); // 8 Years ago
moment("20120620", "YYYYMMDD").fromNow(); // 7 Years ago
moment().startOf('day').fromNow(); // 20 Hours before
moment().endOf('day').fromNow(); // 4 Within hours
moment().startOf('hour').fromNow(); // 1 Hours before
8. Calendar time output instance
moment().subtract(10, 'days').calendar(); // 2019 year 5 month 14 Japan
moment().subtract(6, 'days').calendar(); // Last Saturday night 7 spot 49
moment().subtract(3, 'days').calendar(); // This Tuesday night 7 spot 49
moment().subtract(1, 'days').calendar(); // Last night, 7 spot 49 branch
moment().calendar(); // This evening, 7 spot 49 branch
moment().add(1, 'days').calendar(); // Tomorrow night 7 spot 49 branch
moment().add(3, 'days').calendar(); // Next night 7 spot 49
moment().add(10, 'days').calendar(); // 2019 year 6 month 3 Japan
9. Multi language support for output instances
moment().format('L'); // 2019-05-24
moment().format('l'); // 2019-05-24
moment().format('LL'); // 2019 year 5 month 24 Japan
moment().format('ll'); // 2019 year 5 month 24 Japan
moment().format('LLL'); // 2019 year 5 month 24 On Tuesday night 7 spot 50 branch
moment().format('lll'); // 2019 year 5 month 24 On Tuesday night 7 spot 50 branch
moment().format('LLLL'); // 2019 year 5 month 24 Friday night 7 spot 50 branch
moment().format('llll'); // 2019 year 5 month 24 Friday night 7 spot 50 branch
10. Other practical skills
moment().format("YYYY-MM-DD") // Format and display the current time
`${
moment().subtract("month", +1).format("YYYY-MM")}-01` // Last month's 1 Number
`${
moment().add("month", -1).format("YYYY-MM")}-01` // Last month 1 Number
let M = `${
moment().format("YYYY-MM")}-01` // The 1st of this month
moment(M).add("days", -1).format("YYYY-MM-DD") // At the end of last month
moment().startOf("year").format("YYYY-MM-DD") // The start date of this year ,("2019-01-01")
moment().endOf("year").format("YYYY-MM-DD") // The end date of this year ,("2019-12-31")
//moment Turn it into a timestamp
moment().valueOf()
// Time stamp turn moment
moment(string).format()
See the official documentation for more information : file | Moment.js Chinese net
边栏推荐
- Global industry analysis report of UAV detection radar in 2022
- 关于发布《深圳市培育发展生物医药产业集群行动计划(2022-2025年)》的通知
- 2022.06.04 learning contents
- 数字展厅设计方案的制定要注意这四点
- JS常用时间操作moment.js参考文档
- These four points should be paid attention to when formulating the design scheme of the digital exhibition hall
- Research Report on water jet cutting equipment industry - market status analysis and development prospect forecast
- Esayexcel quick start
- Is it better to use UUID for database primary key or snowflake or something else
- Redis数据类型之list和set以及sorted_set排序
猜你喜欢
![[lingo] operator](/img/c1/b0f7c4285b882278874f9bc6abed95.png)
[lingo] operator

被微软遗弃的神作《扫雷》,竟然被中国玩家玩出花?

Industry application saves 5g? Don't think too much. It's still mobile phone users who save 5g

3775 array completion (ring diagram)

Sum of redis data types hash

The latest Jilin construction safety officer simulation question bank and answers in 2022

Form1 SLA光固化国产仿打印机用的切片软件PreForm下载
![[APIO2022] 火星——构造、状态压缩](/img/ae/877bd8f20082257551c85abdbb1fd6.png)
[APIO2022] 火星——构造、状态压缩

Model deployment

OS实验六【设备管理】
随机推荐
Cache the log in the queue mode, display the log in the queue and display it in different colors (c)
Redis入门一认识与下载
Esayexcel quick start
One's deceased father grind adjusts, read this you will understand!
Genesis public chain is determined to appear at the consensus 2022 Conference
[chapter 65 of the flutter problem series] a solution to setting the maximum height of showmodalbottomsheet in the flutter is invalid
Sum of redis data types hash
关于发布《深圳市培育发展生物医药产业集群行动计划(2022-2025年)》的通知
数字展厅设计方案的制定要注意这四点
UE panorama, problems when encountering the outpout directory
Filter for formatting date and text length
Basic exercise rectangular area intersection
[APIO2022] 火星——构造、状态压缩
[sans titre]
Qt5.9.5+Jetson Nano开发:unknown module(s) in qt designer
Download the slicing software preform for form1 SLA light curing domestic imitation printer
华为设备配置Hub and Spoke
Web安全渗透测试基本流程
Global industry analysis report of Internet subtitle service in 2022
Deep learning: NLP word embedding