# moment
# 安装
bower install moment --save # bower
npm install moment --save # npm
Install-Package Moment.js # NuGet
spm install moment --save # spm
meteor add momentjs:moment # meteor
1
2
3
4
5
2
3
4
5
# 下载
| 多语言支持版本 | 多语言min |
|---|---|
| moment.js | min.js |
# 日期格式化
moment().format('MMMM Do YYYY, h:mm:ss a'); // 十二月 11日 2019, 11:45:47 中午
moment().format('dddd'); // 星期三
moment().format("MMM Do YY"); // 12月 11日 19
moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
moment().format(); // 2019-12-11T11:45:47+08:00
1
2
3
4
5
2
3
4
5
# 相对时间
moment("20111031", "YYYYMMDD").fromNow(); // 8 年前
moment("20120620", "YYYYMMDD").fromNow(); // 7 年前
moment().startOf('day').fromNow(); // 14 小时前
moment().endOf('day').fromNow(); // 10 小时内
moment().startOf('hour').fromNow(); // 33 分钟前
1
2
3
4
5
2
3
4
5
# 日历时间
moment().subtract(10, 'days').calendar(); // 2019年12月1日
moment().subtract(6, 'days').calendar(); // 上周四下午1点34
moment().subtract(3, 'days').calendar(); // 上周日下午1点34
moment().subtract(1, 'days').calendar(); // 昨天下午1点34分
moment().calendar(); // 今天下午1点34分
moment().add(1, 'days').calendar(); // 明天下午1点34分
moment().add(3, 'days').calendar(); // 本周六下午1点34
moment().add(10, 'days').calendar(); // 2019年12月21日
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 多语言支持
moment().format('L'); // 2019-12-11
moment().format('l'); // 2019-12-11
moment().format('LL'); // 2019年12月11日
moment().format('ll'); // 2019年12月11日
moment().format('LLL'); // 2019年12月11日下午1点35分
moment().format('lll'); // 2019年12月11日下午1点35分
moment().format('LLLL'); // 2019年12月11日星期三下午1点35分
moment().format('llll'); // 2019年12月11日星期三下午1点35分
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Moment Timezone
# timezone下载
moment-timezone
min
moment-timezone-with-data
min
moment-timezone-all-years
moment-timezone-all-years-min
bower install moment-timezone --save
npm install moment-timezone --save
Install-Package Moment.Timezone.js
1
2
3
2
3
# Format Dates in Any Timezone
var jun = moment("2014-06-01T12:00:00Z");
var dec = moment("2014-12-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z'); // 5am PDT
dec.tz('America/Los_Angeles').format('ha z'); // 4am PST
jun.tz('America/New_York').format('ha z'); // 8am EDT
dec.tz('America/New_York').format('ha z'); // 7am EST
jun.tz('Asia/Tokyo').format('ha z'); // 9pm JST
dec.tz('Asia/Tokyo').format('ha z'); // 9pm JST
jun.tz('Australia/Sydney').format('ha z'); // 10pm EST
dec.tz('Australia/Sydney').format('ha z'); // 11pm EST
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# Convert Dates Between Timezones
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london = newYork.clone().tz("Europe/London");
newYork.format(); // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format(); // 2014-06-01T17:00:00+01:00
1
2
3
4
5
6
7
2
3
4
5
6
7
# moment与时间戳
const timeStamp = moment().valueOf();
1