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