JavaScript日期时间处理类库Moment.js学习笔记

今天学习了下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/

 TokenOutput
MonthM1 2 ... 11 12
 Mo1st 2nd ... 11th 12th
 MM01 02 ... 11 12
 MMMJan Feb ... Nov Dec
 MMMMJanuary February ... November December
QuarterQ1 2 3 4
 Qo1st 2nd 3rd 4th
Day of MonthD1 2 ... 30 31
 Do1st 2nd ... 30th 31st
 DD01 02 ... 30 31
Day of YearDDD1 2 ... 364 365
 DDDo1st 2nd ... 364th 365th
 DDDD001 002 ... 364 365
Day of Weekd0 1 ... 5 6
 do0th 1st ... 5th 6th
 ddSu Mo ... Fr Sa
 dddSun Mon ... Fri Sat
 ddddSunday Monday ... Friday Saturday
Day of Week (Locale)e0 1 ... 5 6
Day of Week (ISO)E1 2 ... 6 7
Week of Yearw1 2 ... 52 53
 wo1st 2nd ... 52nd 53rd
 ww01 02 ... 52 53
Week of Year (ISO)W1 2 ... 52 53
 Wo1st 2nd ... 52nd 53rd
 WW01 02 ... 52 53
YearYY70 71 ... 29 30
 YYYY1970 1971 ... 2029 2030
 Y1970 1971 ... 9999 +10000 +10001
Note: This complies with the ISO 8601 standard for dates past the year 9999
Week Yeargg70 71 ... 29 30
 gggg1970 1971 ... 2029 2030
Week Year (ISO)GG70 71 ... 29 30
 GGGG1970 1971 ... 2029 2030
AM/PMAAM PM
 aam pm
HourH0 1 ... 22 23
 HH00 01 ... 22 23
 h1 2 ... 11 12
 hh01 02 ... 11 12
 k1 2 ... 23 24
 kk01 02 ... 23 24
Minutem0 1 ... 58 59
 mm00 01 ... 58 59
Seconds0 1 ... 58 59
 ss00 01 ... 58 59
Fractional SecondS0 1 ... 8 9
 SS00 01 ... 98 99
 SSS000 001 ... 998 999
 SSSS ... SSSSSSSSS000[0..] 001[0..] ... 998[0..] 999[0..]
Time zonez or zzEST 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 TimestampX1360013296
Unix Millisecond Timestampx1360013296123

LocalizedFormat

TimeLT8:30 PM
Time with secondsLTS8:30:25 PM
Month numeral, day of month, yearL09/04/1986
 l9/4/1986
Month name, day of month, yearLLSeptember 4 1986
 llSep 4 1986
Month name, day of month, year, timeLLLSeptember 4 1986 8:30 PM
 lllSep 4 1986 8:30 PM
Month name, day of month, day of week, year, timeLLLLThursday, September 4 1986 8:30 PM
 llllThu, 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

转载于:https://my.oschina.net/Tsybius2014/blog/724293

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值