当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript时间处理插件moment.js的使用分析

javascript时间处理插件moment.js的使用分析

2018年10月19日  | 移动技术网IT编程  | 我要评论

因为javascript中没有过多针对时间日期进行处理的原生函数,因此要处理时间,需要自己处理大量代码,效率和正确率都不够高,而moment.js正是针对js时间对象进行快速处理的一个类库,使用它可以轻松快速的处理页面中有关时间处理的问题。

moment.js不依赖任何第三方库,支持字符串、date、时间戳以及数组等格式传入,可以使用提供的函数格式化日期时间,计算相对时间,获取特定时间后的日期时间等等。

moment主要是对原生的data对象进行包装,生成自己定义的包装类,通过moment()即可获取该类的对象,为避免产生混淆,所有的moment操作都是通过这个包装对象对内部的data对象进行操作,从外部来看,我们只需要操作处理包装对象即可完成对内部对象的处理。

moment.js版本:2.22.1

1、获取时间


var now = moment();//获取当前时间
var now = moment(new date())//与上面方法表现一样
var day = moment("1995-12-25");//根据传入字符串获取时间
moment("12-25-1995", "mm-dd-yyyy");//根据自定义格式传入参数
moment("2018-01-01 15:25:32", "yyyy-mm-dd hh:mm:ss");
moment({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123});//传入对象生成时间
[year, month, day, hour, minute, second, millisecond]
moment([2010, 1, 14, 15, 25, 50, 125]);//传入数组生成时间
moment([2010]);        // january 1st
moment([2010, 6]);     // july 1st
moment([2010, 6, 10]); // july 10th
var a = moment([2012]);
var b = moment(a);//复制moment对象

2、合法性校验

moment(参数).isvalid();
new date(2013, 25, 14).tostring(); // "sat feb 14 2015 00:00:00 gmt-0500 (est)"
moment([2015, 25, 35]).format();   // 'invalid date'
moment("2011-10-10t10:20:90").invalidat(); // 5 for seconds,返回无效位置,0years,1months,2days,3hours,4minutes,5seconds,6milliseconds

3、提供时间默认值,按照格式或者确定的属性名称

moment(5, "hh");  // today, 5:00:00.000
moment({hour: 5});  // today, 5:00:00.000
moment({hour: 5, minute: 10});  // today, 5:10.00.000
moment({hour: 5, minute: 10, seconds: 20});  // today, 5:10.20.000
moment({hour: 5, minute: 10, seconds: 20, milliseconds: 300});  // today, 5:10.20.300
moment("4 05:06:07", "dd hh:mm:ss");  // this month, 4th day-of-month, 05:06:07.000

4、取值、赋值,类似jquery的attr取值赋值

//毫秒
moment().millisecond(number);
moment().millisecond(); // number
moment().milliseconds(number);
moment().milliseconds(); // number
//秒
moment().second(number);
moment().second(); // number
moment().seconds(number);
moment().seconds(); // number
//分钟
moment().minute(number);
moment().minute(); // number
moment().minutes(number);
moment().minutes(); // number
//小时
moment().hour(number);
moment().hour(); // number
moment().hours(number);
moment().hours(); // number
//日期,即 天,每7天一循环,超过7天则进到下一周
moment().date(number);
moment().date(); // number
moment().dates(number);
moment().dates(); // number
//星期 day
moment().day(number|string);
moment().day(); // number
moment().days(number|string);
moment().days(); // number
moment().day(-7); // last sunday (0 - 7)
moment().day(7); // next sunday (0 + 7)
moment().day(10); // next wednesday (3 + 7)
moment().day(24); // 3 wednesdays from now (3 + 7 + 7 + 7)
//一年中的第几天
moment().dayofyear(number);
moment().dayofyear(); // number
//一年中的第几个月,从0开始,超过11则进到下一年
moment().month(number|string);
moment().month(); // number
moment().months(number|string);
moment().months(); // number
moment().daysinmonth();//对象月份中的天数
moment("2012-02", "yyyy-mm").daysinmonth() // 29,2月有29天
//年
moment().year(number);
moment().year(); // number
moment().years(number);
moment().years(); // number

5、get/set 取值赋值方式

moment().get('year');
moment().get('month');  // 0 to 11
moment().get('date');
moment().get('hour');
moment().get('minute');
moment().get('second');
moment().get('millisecond');

moment().set('year', 2013);
moment().set('month', 3);  // april
moment().set('date', 1);
moment().set('hour', 13);
moment().set('minute', 20);
moment().set('second', 30);
moment().set('millisecond', 123);

moment().set({'year': 2013, 'month': 3});//数组多项赋值

6、最大值、最小值

var a = moment("2018-01-01");
var b = moment("2018-01-10");
moment.min(a, b);  //a
moment.max(a, b);  //b

7、基本操作

//加法
moment().add(number, string);
moment().add(7, 'days');moment().add(7, 'd');
moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add({days:7,months:1});
moment().add(duration);
moment().add(object);
var duration = moment.duration({'days' : 1});
moment([2012, 0, 31]).add(duration); // february 1
//减法,操作同加法
moment().subtract(number, string);
moment().subtract(duration);
moment().subtract(object);
moment().subtract(7, 'days');
//开始时间
moment().startof(string);//设置值为年月日等的起始时间
moment().month(0).date(1).hours(0).minutes(0).seconds(0).milliseconds(0);与上面语句表现一致
moment().startof('year');    // 设置为一年的开始,即 year-01-01 00:00:00
moment().startof('month');   // 设置为对象月的开始值
moment().startof('quarter');  // 设置为对象季度开始值
moment().startof('week');    // 设置为对象周开始值
moment().startof('day');     // 设置为今天开始,时分秒都为0
moment().startof('hour');    // 设置当前小时开始,分钟0,秒0
moment().startof('minute');  // 设置当前分钟,秒为0
moment().startof('second');  // 设置当前秒与 moment().milliseconds(0)表现一致;
//结束时间,与开始时间取值正好相反
moment().endof(string);
moment().endof("year");//设置为当年的结束值,2018-12-31 23:59:59.999
//其它格式设置同上

8、格式化操作,通过format()函数

moment().format();                                // 2018-05-18t22:18:18+08:00
moment().format("yyyy-mm-dd hh:mm:ss"); // 2018-05-18 22:18:18,输出年月日时分秒
moment().format('d');//从周输出日
moment().format('d');//从月份输出日1 2 ... 30 31
moment().format('dd');//从月份输出日01 02 ... 30 31
moment().format('ddd');//从年输出日1 2 ... 364 365
moment().format('dddd');//从年输出日001 002 ... 364 365

9、时差,2个moment对象之间毫秒差

moment().diff(moment|string|number|date|array);
moment().diff(moment|string|number|date|array, string);
moment().diff(moment|string|number|date|array, string, boolean);
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b) // 86400000毫秒
a.diff(b, 'days') // 1天
a.diff(b, 'years');       // 1,返回取整差值
a.diff(b, 'years', true); // 1.5,返回大概精确差值,小数

10、转换为data,object,数组

moment().todate();//转换原生data
moment().toarray(); // [2013, 1, 4, 14, 40, 16, 154];//转化为原生data对应的数组
moment().toobject()  // {
                     //     years: 2015
                     //     months: 6
                     //     date: 26,
                     //     hours: 1,
                     //     minutes: 53,
                     //     seconds: 14,
                     //     milliseconds: 600
                     // }转换为包含年月日时分秒属性的对象

11、查询比较验证

//是否之前,参数可以是合法时间字符串,moment对象,毫秒数,data对象,合法组成时间的数组等等
moment().isbefore(moment|string|number|date|array);
moment().isbefore(moment|string|number|date|array, string);
moment('2010-10-20').isbefore('2010-10-21'); // true,时间
moment('2010-10-20').isbefore('2010-12-31', 'year'); // false,带比较对象参数
,参数包含year month week day hour minute second
//是否之后,比较方式同上
moment().isafter(moment|string|number|date|array);
moment().isafter(moment|string|number|date|array, string);
//是否相等,比较方式同上
moment().issame(moment|string|number|date|array);
moment().issame(moment|string|number|date|array, string);
//是否之间,比较方式同上,需要多输入一个参数
moment().isbetween(moment-like, moment-like);
moment().isbetween(moment-like, moment-like, string);
// where moment-like is moment|string|number|date|array
moment('2010-10-20').isbetween('2010-10-19', '2010-10-25'); // true
moment('2010-10-20').isbetween('2010-01-01', '2012-01-01', 'year'); // false
//是否闰年
moment().isleapyear();
moment([2000]).isleapyear() // true
moment([2001]).isleapyear() // false
//是否moment对象
moment.ismoment(obj);
//判断是否data对象
moment.isdate(obj);
moment.isdate(); // false
moment.isdate(new date()); // true
moment.isdate(moment()); // false

12、时间段,默认以毫秒为单位,可以从数字、对象、等生成

调用方法:

moment.duration(number, string);
moment.duration(number);
moment.duration(object);
moment.duration(string);
moment.duration(100); // 100 milliseconds,默认毫秒
//指定时间段和类型
moment.duration(2, 'seconds');
moment.duration(2, 'minutes');
moment.duration(2, 'hours');
moment.duration(2, 'days');
moment.duration(2, 'weeks');
moment.duration(2, 'months');
moment.duration(2, 'years');
通过数组形式定义带有多种时间类型的时间段,生成的是moment的duration对象
moment.duration({    seconds: 2,    minutes: 2,    hours: 2,    days: 2,        months: 2,    years: 2});

通过输入时间参数定义

moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59');          //added in 2.3.0

获取时间段中的某一部分单位,例如获取当前时间段的秒,或者获取当前时间段的以秒为单位的

moment.duration('23:59:59');
moment.duration('23:59:59.999');
//毫秒
moment.duration().milliseconds();//获取时间短的毫秒部分
moment.duration().asmilliseconds();//获取时间段以毫秒表示的总时间
//秒
moment.duration().seconds();
moment.duration().asseconds();
//分钟
moment.duration().minutes();
moment.duration().asminutes();
//小时
moment.duration().hours();
moment.duration().ashours();
//天
moment.duration().days();
moment.duration().asdays();
//月
moment.duration().months();
moment.duration().asmonths();
//年
moment.duration().years();
moment.duration().asyears();

时间段的加减法,同时间的加减模式一致

//加法
moment.duration().add(number, string);
moment.duration().add(number);
moment.duration().add(duration);
moment.duration().add(object);
var a = moment.duration(1, 'd');
var b = moment.duration(2, 'd');
a.add(b).days(); // 3

//减法
moment.duration().subtract(number, string);
moment.duration().subtract(number);
moment.duration().subtract(duration);
moment.duration().subtract(object);
var a = moment.duration(3, 'd');
var b = moment.duration(2, 'd');
a.subtract(b).days(); // 1

转换时间段单位,例如将以秒为单位的时间段转换到小时,获取转换单位的总时间值

moment.duration().as(string);//等同于 ashours()\asminutes()等方法
duration.as('hours');
duration.as('minutes');
duration.as('seconds');
duration.as('milliseconds');
moment.duration({    seconds: 62,    minutes: 2,    hours: 2,    days: 32,        months: 1,    years: 2}).as('hours');
19034.050555555557
moment.duration({    seconds: 62,    minutes: 2,    hours: 2,    days: 32,        months: 1,    years: 2}).as('months');
26.054163405895473

时间段各单位取值,获取各单位的单独值

moment.duration().get(string);
duration.get('hours');
duration.get('minutes');
duration.get('seconds');
duration.get('milliseconds');
moment.duration({    seconds: 62,    minutes: 2,    hours: 2,    days: 32,        months: 1,    years: 2}).get('hours');
2
moment.duration({    seconds: 62,    minutes: 2,    hours: 2,    days: 32,        months: 1,    years: 2}).get('months');
2

是否是时间段对象:

moment.isduration(obj);

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网