今天学习了下JavaScript日期时间处理类库Moment.js,本文为学习笔记,我学习时使用的Moment.js版本为2.14.1。
参考资料:http://momentjs.com/docs/
1、获取当前时间
var m = moment();
2、获取ISO-8601格式字符串描述的时间
var m = moment("1995-12-25");
3、通过Format字符串解析指定字符串描述的时间
moment("12-25-1995", "MM-DD-YYYY");
moment('2012-05-25', 'YYYY-MM-DD', true); //第三个布尔型参数决定是否严格匹配
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); //使用第一个可合法转换的Format
Format格式参考:http://momentjs.com/docs/#/parsing/string-format/
4、检查日期时间是否合法
moment("2010 2 29", "YYYY MM DD").isValid();
返回true时为合法时间,否则为非法的时间
以下情况下将认定时间为非法:
1)溢出(overflow),如第13月、某月第32日、某年第367天等
2)非法的月份名(invalidMonth),如moment('Marbruary', 'MMMM');
3)对象为空(empty),转换函数转换失败时对象为空,如moment('this is nonsense');
4)输入为空(nullInput),转换函数传入null,如moment(null);
5)格式错误(invalidFormat)
6)用户定义的非法类型(userInvalidated),如moment.invalid();
如果moment被判定为invalid,可以通过invalidAt()函数查看是哪一项出了问题,返回值为:
0(年)、1(月)、2(日)、3(时)、4(分)、5(秒)、6(毫秒)
5、通过指定年、月、日、时、分、秒、毫秒创建moment
moment({ hour:15, minute:10 });
moment({ y :2010, M :3, d :5, h :15, m :10, s :3, ms :123});
moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123});
moment({ years:2010, months:3, days:5, hours:15, minutes:10, seconds:3, milliseconds:123});
moment({ years:2010, months:3, date:5, hours:15, minutes:10, seconds:3, milliseconds:123});
moment({ years:'2010', months:'3', date:'5', hours:'15', minutes:'10', seconds:'3', milliseconds:'123'});
6、使用Unix时间戳赋值创建moment
moment(1318781876406);
7、使用Date对象创建moment
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
8、使用数组创建moment
moment([2010]); // January 1st
moment([2010, 6]); // July 1st
moment([2010, 6, 10]); // July 10th
moment([2010, 1, 14, 15, 25, 50, 125]);
9、moment对象的复制
var a = moment([2012]);
var b = moment(a);
10、creationData函数
moment对象建立后,所有输入可以通过creationData()获取
moment("2013-01-02", "YYYY-MM-DD", true).creationData() === {
input: "2013-01-02",
format: "YYYY-MM-DD",
locale: Locale obj,
isUTC: false,
strict: true
}
11、获取(get)与设置(set)
moment中各属性可通过对应函数直接获取,如
moment().seconds(30) === new Date().setSeconds(30);
moment().seconds() === new Date().getSeconds();
方法列表详见:http://momentjs.com/docs/#/get-set/
12、对时间的操作
1)add:向后取指定长度的时间
2)substract:向前取指定长度的时间
3)startOf、endOf:获取当年/月/...的起/终点时间
4)max、min:获取当前时间与另一时间相较更大/小者
5)local:获取本地时间
6)utc:获取UTC时间
13、Format字符串
Format字符串的规则可参考:http://momentjs.com/docs/#/displaying/format/
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 | |
Quarter | Q | 1 2 3 4 |
Qo | 1st 2nd 3rd 4th | |
Day of Month | D | 1 2 ... 30 31 |
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 | |
dd | Su Mo ... Fr Sa | |
ddd | Sun Mon ... Fri Sat | |
dddd | Sunday Monday ... Friday Saturday | |
Day of Week (Locale) | e | 0 1 ... 5 6 |
Day of Week (ISO) | E | 1 2 ... 6 7 |
Week of Year | w | 1 2 ... 52 53 |
wo | 1st 2nd ... 52nd 53rd | |
ww | 01 02 ... 52 53 | |
Week of Year (ISO) | 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 | |
Y | 1970 1971 ... 9999 +10000 +10001 Note: This complies with the ISO 8601 standard for dates past the year 9999 |
|
Week Year | gg | 70 71 ... 29 30 |
gggg | 1970 1971 ... 2029 2030 | |
Week Year (ISO) | GG | 70 71 ... 29 30 |
GGGG | 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 | |
k | 1 2 ... 23 24 | |
kk | 01 02 ... 23 24 | |
Minute | m | 0 1 ... 58 59 |
mm | 00 01 ... 58 59 | |
Second | s | 0 1 ... 58 59 |
ss | 00 01 ... 58 59 | |
Fractional Second | S | 0 1 ... 8 9 |
SS | 00 01 ... 98 99 | |
SSS | 000 001 ... 998 999 | |
SSSS ... SSSSSSSSS | 000[0..] 001[0..] ... 998[0..] 999[0..] | |
Time zone | z or zz | EST CST ... MST PST Note: as of 1.6.0, the z/zz format tokens have been deprecated from plain moment objects. Read more about it here. However, they do work if you are using a specific time zone with the moment-timezone addon. |
Z | -07:00 -06:00 ... +06:00 +07:00 | |
ZZ | -0700 -0600 ... +0600 +0700 | |
Unix Timestamp | X | 1360013296 |
Unix Millisecond Timestamp | x | 1360013296123 |
LocalizedFormat
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 |
在Format字符串中,方括号[]内的文字将不用于翻译
14、几个函数
1)fromNow返回指定moment到现在的时间间隔,from返回指定moment到另一moment的时间间隔
2)toNow和to类似fromNow和from,只是输出文字有不同
3)diff用于返回两个moment间的时间间隔,如表达式a.diif(b,'days')返回a时间与b时间相隔的天数
4)daysInMonth返回指定月有多少日,如:
moment("2012-02", "YYYY-MM").daysInMonth() // 返回29
moment("2012-01", "YYYY-MM").daysInMonth() // 返回31
5)toDate、toArray、toJSON、toISOString、toObject、toString用于将moment转换为其他不同的数据类型
15、几个函数(2)
1)isBefore用于判断一个moment是否在另一moment之前
2)isSame用于判断两moment是否相等
3)isAfter用于判断一个moment是否在另一moment之后
4)isSameOrBefore用于判断一个moment是否不在另一moment之后
5)isSameOrAfter用于判断一个moment是否不在另一moment之前
6)isBetween用于判断一个moment是否在另外两个moment之间
7)isDST用于判断某一moments是否在夏令时(Daylight Saving Time)
8)isLeapYear用于判断某一年是否是闰年
9)isMoment用于判断某一对象是否为Moment类型
10)isDate用于判断某一变量是否为JS固有的数据对象
END
来源:oschina
链接:https://my.oschina.net/u/1425762/blog/724293