当前位置:网站首页>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 PMAM PM
a Lowercase am pmam 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
XUnix 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

原网站

版权声明
本文为[Back end regular developer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100812207995.html