moment.js 日期包装类
Moment.js
1创建时间对象
moment(); 当前时间
moment(new Date(2020, 3, 7)); date 对象创建
moment(1318781876406); 时间戳创建
moment("12-25-1995", "MM-DD-YYYY"); 日期文字+日期格式
moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
moment([year, month , date , hours , minutes , seconds , milliseconds ]); 数组创建
moment.unix(Number) unix时间
日期格式表
Input |
Output |
M or MM |
Month Number (1 - 12) |
MMM or MMMM |
Month Name (In currently language set by `moment.lang()`) |
D or DD |
Day of month |
DDD or DDDD |
Day of year |
d, dd, ddd, or dddd |
Day of week (NOTE: these tokens are not used to create the date, as there are 4-5 weeks in a month, and it would be impossible to get the date based off the day of the week) |
YY |
2 digit year (if greater than 70, will return 1900's, else 2000's) |
YYYY |
4 digit year |
a or A |
AM/PM |
H, HH |
24 hour time |
h, or hh |
12 hour time (use in conjunction with a or A) |
m or mm |
Minutes |
s or ss |
Seconds |
S |
Deciseconds (1/10th of a second) |
SS |
Centiseconds (1/100th of a second) |
SSS |
Milliseconds (1/1000th of a second) |
Z or ZZ |
Timezone offset as `+0700` or `+07:30` Available in version *1.2.0* |
Key |
Shorthand |
year |
y |
month |
M |
week |
w |
day |
d |
hour |
h |
minutes |
m |
seconds |
s |
millisecond |
ms |
2 修改时间对象
2.1 加减
moment().add(1000) 加毫秒
moment().add('days', 7) 加7天
moment().subtract('days', 7); 减7天
moment().add({days:-7,months:1}); 加1月,减7天
2.2设值
moment().seconds(30); year/month/date/day/minutes/seconds 方法
moment().seconds(); 取值
设首/尾
moment().startOf('year'); // set to Jan 1 12:00:00.000 pm this year
moment().startOf('month'); // set to the first of this month, 12:00:00.000 pm
moment().startOf('day'); // set to 12:00:00.000 pm today
moment().startOf('hour'); // set to now, but with 0 mins, 0 secs, and 0 ms
moment().startOf('minute'); // set to now, but with 0 seconds and 0 milliseconds
moment().startOf('second'); // same as moment().milliseconds(0);
moment().endOf('year');
moment().sod(); 当天头
moment().eod(); 当天尾
3 格式化输出
moment().format("dddd, MMMM Do YYYY, h:mm:ss a");
Token |
Output |
Month |
|
M |
1 2 ... 11 12 |
Mo |
1st 2nd ... 11th 12th |
MM |
01 02 ... 11 12 |
MMM |
Jan Feb ... Nov Dec |
MMMM |
January February ... November December |
Day of Month |
|
D |
1 2 ... 30 30 |
Do |
1st 2nd ... 30th 31st |
DD |
01 02 ... 30 31 |
Day of Year |
|
DDD |
1 2 ... 364 365 |
DDDo |
1st 2nd ... 364th 365th |
DDDD |
001 002 ... 364 365 |
Day of Week |
|
d |
0 1 ... 5 6 |
do |
0th 1st ... 5th 6th |
ddd |
Sun Mon ... Fri Sat |
dddd |
Sunday Monday ... Friday Saturday |
Week of Year |
|
w |
1 2 ... 52 53 |
wo |
1st 2nd ... 52nd 53rd |
ww |
01 02 ... 52 53 |
Year |
|
YY |
70 71 ... 29 30 |
YYYY |
1970 1971 ... 2029 2030 |
AM/PM |
|
A |
AM PM |
a |
am pm |
Hour |
|
H |
0 1 ... 22 23 |
HH |
00 01 ... 22 23 |
h |
1 2 ... 11 12 |
hh |
01 02 ... 11 12 |
Minute |
|
m |
0 1 ... 58 59 |
mm |
00 01 ... 58 59 |
Second |
|
s |
0 1 ... 58 59 |
ss |
00 01 ... 58 59 |
Timezone |
|
z or zz |
EST CST ... MST PST **Note:** as of *1.6.0*, the z/zz format tokens have been deprecated. [Read more about it here.](https://github.com/timrwood/moment/issues/162) |
Z |
-07:00 -06:00 ... +06:00 +07:00 |
ZZ |
-0700 -0600 ... +0600 +0700 Z and ZZ are available in version *1.2.0* |
Localized date format |
|
LT |
8:30 PM LT is available in version *1.3.0* |
L |
07/10/1986 |
LL |
July 10 1986 |
LLL |
July 10 1986 8:30 PM |
LLLL |
Saturday, July 10 1986 8:30 PM L, LL, LLL, LLLL are available in version *1.3.0* |
4 比较
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
- a.diff(b, 'days') // 1
5 汉化
moment.lang('zh-cn', {
months : "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
monthsShort : "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"),
weekdays : "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"),
weekdaysShort : "周日_周一_周二_周三_周四_周五_周六".split("_"),
weekdaysMin : "日_一_二_三_四_五_六".split("_"),
longDateFormat : {
LT : "Ah点mm",
L : "YYYY年MMMD日",
LL : "YYYY年MMMD日",
LLL : "YYYY年MMMD日LT",
LLLL : "YYYY年MMMD日ddddLT",
l : "YYYY年MMMD日",
ll : "YYYY年MMMD日",
lll : "YYYY年MMMD日LT",
llll : "YYYY年MMMD日ddddLT"
},
meridiem : function (hour, minute, isLower) {
if (hour < 9) {
return "早上";
} else if (hour < 11 && minute < 30) {
return "上午";
} else if (hour < 13 && minute < 30) {
return "中午";
} else if (hour < 18) {
return "下午";
} else {
return "晚上";
}
},
calendar : {
sameDay : '[今天]LT',
nextDay : '[明天]LT',
nextWeek : '[下]ddddLT',
lastDay : '[昨天]LT',
lastWeek : '[上]ddddLT',
sameElse : 'L'
},
relativeTime : {
future : "%s内",
past : "%s前",
s : "几秒",
m : "1分钟",
mm : "%d分钟",
h : "1小时",
hh : "%d小时",
d : "1天",
dd : "%d天",
M : "1个月",
MM : "%d个月",
y : "1年",
yy : "%d年"
}
});
6 重写扩展
moment.months monthsShort weekdays weekdaysShort weekdaysMin longDateFormat relativeTime 扩展自定义
7 其他
零碎的api
clone 复制
isValid 验证合法
daysInMonth 每月天数
isDST 是否休息日
isLeapYear 闰年
源码下载:https://github.com/timrwood/moment/