欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

moment.js使用方法總結(jié)(全網(wǎng)最全)

 更新時(shí)間:2024年03月25日 10:56:00   作者:Cookie王  
日常開發(fā)中通常會(huì)對(duì)時(shí)間進(jìn)行下面這幾個(gè)操作,比如獲取時(shí)間,設(shè)置時(shí)間,格式化時(shí)間,比較時(shí)間等等,下面這篇文章主要給大家介紹了關(guān)于moment.js使用方法的相關(guān)資料,需要的朋友可以參考下

前言

Moment.js是一個(gè)輕量級(jí)的JavaScript時(shí)間庫(kù),它方便了日常開發(fā)中對(duì)時(shí)間的操作,提高了開發(fā)效率。日常開發(fā)中,通常會(huì)對(duì)時(shí)間進(jìn)行下面這幾個(gè)操作:比如獲取時(shí)間,設(shè)置時(shí)間,格式化時(shí)間,比較時(shí)間等等。下面就是我對(duì)moment.js使用過(guò)程中的整理,方便以后查閱。

一、引入moment.js

1.Node.js方式引入

(1)安裝

npm install moment 或者 yarn add moment

(2)引入

// require 方式
var moment = require('moment');

// import 方式
import moment from 'moment'; 

2.瀏覽器方式引入

<script src="moment.js"></script>

二、設(shè)定moment區(qū)域?yàn)橹袊?guó)

// require 方式
require('moment/locale/zh-cn')
moment.locale('zh-cn'); 

// import 方式
import 'moment/locale/zh-cn'
moment.locale('zh-cn');   

三、使用

1.獲取時(shí)間

(1)獲取當(dāng)前時(shí)間

moment()

(2)獲取今天0時(shí)0分0秒

moment().startOf('day')

(3)獲取本周第一天(周日)0時(shí)0分0秒

moment().startOf('week')

(4)獲取本周周一0時(shí)0分0秒

moment().startOf('isoWeek')

(5)獲取當(dāng)前月第一天0時(shí)0分0秒

moment().startOf('month')

(6)獲取今天23時(shí)59分59秒

moment().endOf('day')

(7)獲取本周最后一天(周六)23時(shí)59分59秒

moment().endOf('week')

(8)獲取本周周日23時(shí)59分59秒

moment().endOf('isoWeek')

(9)獲取當(dāng)前月最后一天23時(shí)59分59秒

moment().endOf('month')

(10)獲取當(dāng)前月的總天數(shù)

moment().daysInMonth() 

(11)獲取時(shí)間戳(以秒為單位)

moment().format('X') // 返回值為字符串類型
moment().unix() // 返回值為數(shù)值型

(12)獲取時(shí)間戳(以毫秒為單位)

moment().format('x') // 返回值為字符串類型
moment().valueOf() // 返回值為數(shù)值型

(13)獲取年份

moment().year()
moment().get('year')

(14)獲取月份

moment().month()  // (0~11, 0: January, 11: December)
moment().get('month')

(15)獲取一個(gè)月中的某一天

moment().date()
moment().get('date')

(16)獲取一個(gè)星期中的某一天

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)獲取小時(shí)

moment().hours()
moment().get('hours')

(18)獲取分鐘

moment().minutes()
moment().get('minutes')

(19)獲取秒數(shù)

moment().seconds()
moment().get('seconds')

(20)獲取當(dāng)前的年月日時(shí)分秒

moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds]
moment().toObject() // {years: xxxx, months: x, date: xx ...}

2.設(shè)置時(shí)間

(1)設(shè)置年份

moment().year(2019)
moment().set('year', 2019)
moment().set({year: 2019})

(2)設(shè)置月份

moment().month(11)  // (0~11, 0: January, 11: December)
moment().set('month', 11) 

(3)設(shè)置某個(gè)月中的某一天

moment().date(15)
moment().set('date', 15)

(4)設(shè)置某個(gè)星期中的某一天

moment().weekday(0) // 設(shè)置日期為本周第一天(周日)
moment().isoWeekday(1) // 設(shè)置日期為本周周一
moment().set('weekday', 0)
moment().set('isoWeekday', 1)

(5)設(shè)置小時(shí)

moment().hours(12)
moment().set('hours', 12)

(6)設(shè)置分鐘

moment().minutes(30)
moment().set('minutes', 30)

(7)設(shè)置秒數(shù)

moment().seconds(30)
moment().set('seconds', 30)

(8)年份+1

moment().add(1, 'years')
moment().add({years: 1})

(9)月份+1

moment().add(1, 'months')

(10)日期+1

moment().add(1, 'days')

(11)星期+1

moment().add(1, 'weeks')

(12)小時(shí)+1

moment().add(1, 'hours')

(13)分鐘+1

moment().add(1, 'minutes')

(14)秒數(shù)+1

moment().add(1, 'seconds')
#### (15)年份-1
```js
moment().subtract(1, 'years')
moment().subtract({years: 1})

(16)月份-1

moment().subtract(1, 'months')

(17)日期-1

moment().subtract(1, 'days')

(18)星期-1

moment().subtract(1, 'weeks')

(19)小時(shí)-1

moment().subtract(1, 'hours')

(20)分鐘-1

moment().subtract(1, 'minutes')

(21)秒數(shù)-1

moment().subtract(1, 'seconds')

3.格式化時(shí)間

格式代碼說(shuō)明返回值例子
M數(shù)字表示的月份,沒(méi)有前導(dǎo)零1到12
MM數(shù)字表示的月份,有前導(dǎo)零01到12
MMM三個(gè)字母縮寫表示的月份Jan到Dec
MMMM月份,完整的文本格式January到December
Q季度1到4
D月份中的第幾天,沒(méi)有前導(dǎo)零1到31
DD月份中的第幾天,有前導(dǎo)零01到31
d星期中的第幾天,數(shù)字表示0到6,0表示周日,6表示周六
ddd三個(gè)字母表示星期中的第幾天Sun到Sat
dddd星期幾,完整的星期文本從Sunday到Saturday
w年份中的第幾周如42:表示第42周
YYYY四位數(shù)字完整表示的年份如:2014 或 2000
YY兩位數(shù)字表示的年份如:14 或 98
A大寫的AM PMAM PM
a小寫的am pmam pm
HH小時(shí),24小時(shí)制,有前導(dǎo)零00到23
H小時(shí),24小時(shí)制,無(wú)前導(dǎo)零0到23
hh小時(shí),12小時(shí)制,有前導(dǎo)零00到12
h小時(shí),12小時(shí)制,無(wú)前導(dǎo)零0到12
m沒(méi)有前導(dǎo)零的分鐘數(shù)0到59
mm有前導(dǎo)零的分鐘數(shù)00到59
s沒(méi)有前導(dǎo)零的秒數(shù)1到59
ss有前導(dǎo)零的描述01到59
XUnix時(shí)間戳1411572969

(1)格式化年月日: 'xxxx年xx月xx日'

moment().format('YYYY年MM月DD日')

(2)格式化年月日: 'xxxx-xx-xx'

moment().format('YYYY-MM-DD')

(3)格式化時(shí)分秒(24小時(shí)制): 'xx時(shí)xx分xx秒'

moment().format('HH時(shí)mm分ss秒')

(4)格式化時(shí)分秒(12小時(shí)制):'xx:xx:xx am/pm'

moment().format('hh:mm:ss a')

(5)格式化時(shí)間戳(以毫秒為單位)

moment().format('x') // 返回值為字符串類型

4.比較時(shí)間

(1)獲取兩個(gè)日期之間的時(shí)間差

let start_date = moment().subtract(1, 'weeks')
let end_date = moment()

end_date.diff(start_date) // 返回毫秒數(shù)

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.轉(zhuǎn)化為JavaScript原生Date對(duì)象

moment().toDate()
new Date(moment())

6.日期格式化

輸出實(shí)例

moment().format('MMMM Do YYYY, h:mm:ss a'); // 五月 24日 2019, 7:47:43 晚上
moment().format('dddd');                    // 星期五
moment().format("MMM Do YY");               // 5月 24日 19
moment().format('YYYY [escaped] YYYY');     // 2019 escaped 2019
moment().format();                          // 2019-05-24T19:47:43+08:00

7.相對(duì)時(shí)間

輸出實(shí)例

moment("20111031", "YYYYMMDD").fromNow(); // 8 年前
moment("20120620", "YYYYMMDD").fromNow(); // 7 年前
moment().startOf('day').fromNow();        // 20 小時(shí)前
moment().endOf('day').fromNow();          // 4 小時(shí)內(nèi)
moment().startOf('hour').fromNow();       // 1 小時(shí)前

8.日歷時(shí)間

輸出實(shí)例

moment().subtract(10, 'days').calendar(); // 2019年5月14日
moment().subtract(6, 'days').calendar();  // 上周六晚上7點(diǎn)49
moment().subtract(3, 'days').calendar();  // 本周二晚上7點(diǎn)49
moment().subtract(1, 'days').calendar();  // 昨天晚上7點(diǎn)49分
moment().calendar();                      // 今天晚上7點(diǎn)49分
moment().add(1, 'days').calendar();       // 明天晚上7點(diǎn)49分
moment().add(3, 'days').calendar();       // 下周一晚上7點(diǎn)49
moment().add(10, 'days').calendar();      // 2019年6月3日

9.多語(yǔ)言支持

輸出實(shí)例

moment().format('L');    // 2019-05-24
moment().format('l');    // 2019-05-24
moment().format('LL');   // 2019年5月24日
moment().format('ll');   // 2019年5月24日
moment().format('LLL');  // 2019年5月24日晚上7點(diǎn)50分
moment().format('lll');  // 2019年5月24日晚上7點(diǎn)50分
moment().format('LLLL'); // 2019年5月24日星期五晚上7點(diǎn)50分
moment().format('llll'); // 2019年5月24日星期五晚上7點(diǎn)50分

10.其它實(shí)用技巧

輸出實(shí)例:

moment().format("YYYY-MM-DD") // 格式化顯示當(dāng)前時(shí)間
`${moment().subtract("month", +1).format("YYYY-MM")}-01` // 上一個(gè)月的1號(hào)
`${moment().add("month", -1).format("YYYY-MM")}-01`  // 還是上一個(gè)月1號(hào) 
let M = `${moment().format("YYYY-MM")}-01` // 本月一號(hào)
moment(M).add("days", -1).format("YYYY-MM-DD") // 上一個(gè)月月底 
moment().startOf("year").format("YYYY-MM-DD")  // 本年的的開始日期,("2019-01-01")
moment().endOf("year").format("YYYY-MM-DD")  // 本年的的結(jié)束日期,("2019-12-31")
// moment 轉(zhuǎn)成時(shí)間戳
moment().valueOf()
// 時(shí)間戳 轉(zhuǎn) moment
moment(string).format()

// 解決Moment格式化時(shí)間出現(xiàn)時(shí)區(qū)差的問(wèn)題
// `utcOffset()` 接收數(shù)字,時(shí)間偏移量,單位:分鐘
// 北京時(shí)間東八區(qū)時(shí)間,比零時(shí)區(qū)早8個(gè)小時(shí)(480分鐘),所以應(yīng)該加上480分鐘
Moment(date).utcOffset(480).format('YYYY-MM-DD HH:mm:ss');

更多可參見(jiàn)官方文檔:Moment.js 中文網(wǎng) | 開發(fā)文檔

總結(jié)

到此這篇關(guān)于moment.js使用方法總結(jié)的文章就介紹到這了,更多相關(guān)moment.js使用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論