Cheat Sheet速查表
跳转到主要内容

Moment.js 速查表

本文根据 Devhints / Moment.js 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2018-09-15。

解析

m = moment('2013-03-01', 'YYYY-MM-DD')

使用指定的格式解析给定的日期。返回一个 moment 对象。

格式化

m.format()            // "2013-03-01T00:00:00+01:00"
m.format('dddd')      // "Friday"
m.format('MMM Do YY') // "Mar 1st 13"
m.fromNow()           // "7 years ago"
m.calendar()          // "03/01/2013"

添加

m.add(1, 'day')
m.subtract(2, 'days')
m.startOf('day')
m.endOf('day')
m.startOf('hour')

国际化

.format('L')      // 06/09/2014
.format('l')      // 6/9/2014
.format('LL')     // June 9 2014
.format('ll')     // Jun 9 2014
.format('LLL')    // June 9 2014 9:32 PM
.format('lll')    // Jun 9 2014 9:32 PM
.format('LLLL')   // Monday, June 9 2014 9:32 PM
.format('llll')   // Mon, Jun 9 2014 9:32 PM

参见 datetime 了解更多。

格式化

示例

日期

示例输出
YYYY-MM-DD2014-01-01
dddd, MMMM Do YYYY2014 年 5 月 16 日,星期五
dddd [the] Do [of] MMMM5 月 16 日,星期五

时间

示例输出
hh:mm a下午 12:30

用于 Moment.js 以及 date-fns/格式.类似于 Java SimpleDateFormat.

日期

符号示例区域
d0..6星期几
ddSu
dddSun
ddddSunday
YY13年
YYYY2013
M1..12 (1 月为 1)月
Mo1st..12th
MM01..12 (1 月为 1)
MMMJan
MMMMJanuary
Q1..4季度
Qo1st..4th
D1..31日
Do1st..31st
DD01..31
DDD1..365一年中的第几天
DDDo1st..365th
DDDD001..365
w1..53一年中的第几周
wo1st..53rd
ww01..53

时间

符号示例区域
H0..2324 小时制小时
HH00..23
h1..1212 小时制小时
hh01..12
m0..59分钟
mm00..59
s0..59秒
ss00..59
aamAM/PM
AAM
Z+07:00时区偏移量
ZZ+0730
S0..9十分之一秒
SS00..99百分之一秒
SSS000..999毫秒
XUnix 时间戳
x毫秒级 Unix 时间戳

预设

示例输出
LT8:30 PM
LTS8:30:25 PM
LL1985 年 8 月 2 日
ll1985 年 8 月 2 日
LLL1985 年 8 月 2 日 08:30 PM
lll1985 年 8 月 2 日 08:30 PM
LLLL1985 年 8 月 2 日星期四 08:30 PM
llll1985 年 8 月 2 日星期四 08:30 PM

参考资料

替代方案

另请参阅