Moment.js ,JavaScript 日期處理類庫。 旨在在瀏覽器和Node.js中工作。
目前,以下瀏覽器用於ci系統:Windows 7上的IE8,IE9,Windows XP上的穩定Chrome,Mac上的Safari 10.8和Linux上的穩定Firefox。
引入方式
npm install moment
var moment = require('moment');
moment().format();
瀏覽器
<script src="moment.js"></script>
<script>
moment().format();
</script>
webpack
npm install moment --save
webpack.config.js中加入以下代碼
plugins: [
new MomentLocalesPlugin(),
],
import moment from 'moment'
import 'moment/locale/zh-cn' //引入中文包
使用
日期格式化
Input | Example | Description |
---|---|---|
YYYY |
2014 |
4位數年份(4 digit year) |
YY |
14 |
2位數年份(2 digit year) |
Q |
1..4 |
季度(Quarter of year. Sets month to first month in quarter.) |
M MM |
1..12 |
月(Month number) |
MMM MMMM |
Jan..December |
moment.locale()方法中設置的月份名稱( Month name in locale set by moment.locale() ) |
D DD |
1..31 |
月份中的天數(Day of month) |
Do |
1st..31st |
月份中的有序天數(Day of month with ordinal) |
DDD DDDD |
1..365 |
一年中的天數(Day of year) |
X |
1410715640.579 |
UNix時間戳(Unix timestamp) |
x |
1410715640579 |
UNix毫秒時間戳(Unix ms timestamp) |
Input | Example | Description |
---|---|---|
gggg |
2014 |
(本地時間顯示4位數年)Locale 4 digit week year |
gg |
14 |
(本地時間顯示2位數年)Locale 2 digit week year |
w ww |
1..53 |
(本地時間顯示一年中的54週中某一週)Locale week of year |
e |
1..7 |
(本地時間顯示一週中某一天)Locale day of week |
ddd dddd |
Mon...Sunday |
(monent.localse()方法中設置的周名稱)Day name in locale set by moment.locale() |
GGGG |
2014 |
(ISO標準時間顯示4位數年)ISO 4 digit week year |
GG |
14 |
(ISO標準時間顯示2位數年)ISO 2 digit week year |
W WW |
1..53 |
(ISO標準時間顯示54週中某一週)ISO week of year |
E |
1..7 |
(ISO標準時間顯示一週中某一天)ISO day of week |
Input | Example | Description |
---|---|---|
H HH |
0..23 |
(24小時時間)24 hour time |
h hh |
1..12 |
(12小時時間)12 hour time used with a A . |
a A |
am pm |
(上午或下午)Post or ante meridiem |
m mm |
0..59 |
(分鐘)Minutes |
s ss |
0..59 |
(秒)Seconds |
S |
0..9 |
Tenths of a second |
SS |
0..99 |
Hundreds of a second |
SSS |
0..999 |
Thousandths of a second |
SSSS |
0000..9999 |
fractional seconds |
Z ZZ |
+12:00 |
Offset from UTC as +-HH:mm , +-HHmm , or Z |
moment().format('MMMM Do YYYY, h:mm:ss a'); // 四月 22日 2019, 2:49:53 下午
moment().format('dddd'); // 星期一
moment().format("MMM Do YY"); // 4月 22日 19
moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019
moment().format(); // 2019-04-22T14:49:53+08:00
相對時間
moment("20111031", "YYYYMMDD").fromNow(); // 7 年前
moment("20120620", "YYYYMMDD").fromNow(); // 7 年前
moment().startOf('day').fromNow(); // 15 小時前
moment().endOf('day').fromNow(); // 9 小時內
moment().startOf('hour').fromNow(); // 1 小時前
日曆時間
moment().subtract(10, 'days').calendar(); // 2019年4月12日
moment().subtract(6, 'days').calendar(); // 上週二下午2點51
moment().subtract(3, 'days').calendar(); // 上週五下午2點51
moment().subtract(1, 'days').calendar(); // 昨天下午2點51分
moment().calendar(); // 今天下午2點51分
moment().add(1, 'days').calendar(); // 明天下午2點51分
moment().add(3, 'days').calendar(); // 本週四下午2點51
moment().add(10, 'days').calendar(); // 2019年5月2日
多語言支持
Time | LT | 8:30 PM |
Time with seconds | LTS | 8:30:25 PM |
/Month numeral, day of month, year | L | 09/04/1986 |
l | 9/4/1986 | |
Month name, day of month, year | LL | September 4 1986 |
ll | Sep 4 1986 | |
Month name, day of month, year, time | LLL | September 4 1986 8:30 PM |
lll | Sep 4 1986 8:30 PM | |
Month name, day of month, day of week, year, time | LLLL | Thursday, September 4 1986 8:30 PM |
llll | Thu, Sep 4 1986 8:30 PM |
moment().format('L'); // 2019-04-22
moment().format('l'); // 2019-04-22
moment().format('LL'); // 2019年4月22日
moment().format('ll'); // 2019年4月22日
moment().format('LLL'); // 2019年4月22日下午2點51分
moment().format('lll'); // 2019年4月22日下午2點51分
moment().format('LLLL'); // 2019年4月22日星期一下午2點51分
moment().format('llll'); // 2019年4月22日星期一下午2點51分
時差
Range | Key | Sample Output(範例) |
---|---|---|
0 to 45 seconds | s | a few seconds ago |
45 to 90 seconds | m | a minute ago |
90 seconds to 45 minutes | mm | 2 minutes ago ... 45 minutes ago |
45 to 90 minutes | h | an hour ago |
90 minutes to 22 hours | hh | 2 hours ago ... 22 hours ago |
22 to 36 hours | d | a day ago |
36 hours to 25 days | dd | 2 days ago ... 25 days ago |
25 to 45 days | M | a month ago |
45 to 345 days | MM | 2 months ago ... 11 months ago |
345 to 545 days (1.5 years) | y | a year ago |
546 days+ | yy | 2 years ago ... 20 years ago |
時差(之前,現在爲基準)
moment().fromNow(); //幾秒前
moment().fromNow(Boolean); //幾秒
//如果傳遞true,則可以獲得不帶後綴的值。
moment([2007, 0, 29]).fromNow(); // 4年前
moment([2007, 0, 29]).fromNow(true); // 4年
時差之前
var a = moment([2007, 0, 20]);
var b = moment([2007, 0, 29]);
a.from(b) // "9天前"
時差之後現在爲基準
moment([2007, 0, 29]).toNow(); // 12 年內
moment([2007, 0, 29]).toNow(true); // 12 年
時差之後
var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.to(b) // "in a day"
時差毫秒
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
默認情況下,moment#diff將返回向下舍入的數字。 如果需要浮點數,請將true作爲第三個參數傳遞。 在2.0.0之前,moment#diff返回舍入的數字,而不是向下舍入的數字。
var a = moment([2008, 6]);
var b = moment([2007, 0]);
a.diff(b, 'years'); // 1
a.diff(b, 'years', true); // 1.5