moment的常见用法总结

1,118 阅读4分钟

前端很多场景都会涉及到对时间的处理,本文就对moment的常见用法做一些总结

moment需要先引入,再使用,其本身是个对象,内置了一些方法,可以把字符串,Date对象转化成moment对象

指定语言;不然可能引起一些不必要的bug

# 记得指定为对应的语言;
 moment.locale('zh-cn')

格式化

moment().format();
# 2020-03-14T19:14:05+08:00
 
moment().format('YYYY-MM-DD HH:mm:ss');
# 2020-03-14 19:23:29

获取时间戳

# 精确到毫秒
 
moment().valueOf()
## 1584182611042 ;返回值为数值类型
moment().format('x') 
## 返回值为字符串类型
 
 
# 精确到秒 
moment().unix()
##  1584182618 精确到秒  返回值为数值类型
moment().format('X') 
##  返回值为字符串类型

生成指定时间的moment

moment("1995-12-25");
 
# 带格式
# 解析器会忽略非字母和数字的字符,因此以下两个都将会返回相同的东西。
moment("12-25-1995", "MM-DD-YYYY");
moment("12/25/1995", "MM-DD-YYYY");

获取对象

moment().toObject();
# 返回一个包括:年、月、日、时、分、秒、毫秒的对象
# {
    years: 2020
    months: 2
    date: 14
    hours: 18
    minutes: 47
    seconds: 56
    milliseconds: 526
}

获取时间

# 获取今天000moment().startOf('day')
 
# 获取本周第一天(周日)000moment().startOf('week')
 
# 获取本周周一000moment().startOf('isoWeek')
 
# 获取当前月第一天000moment().startOf('month')
 
# 获取指定日期的000moment('2019-10-20').startOf('day')
 
# 获取今天235959moment().endOf('day')
 
# 获取本周最后一天(周六)235959moment().endOf('week')
 
# 获取本周周日235959moment().endOf('isoWeek')
 
# 获取当前月最后一天235959moment().endOf('month')

获取当月第一天是星期几

# 用于设置星期几,其中星期日为 0、星期六为 6
moment().startOf('month').day()

获取前n天 / 后n天

moment().add(7, 'days');
moment().subtract(7, 'days')

比较两个时间的大小

# 第二个参数用于确定精度,且不仅仅是要检查的单个值,因此使用 day 将会检查年份、月份、日期。
 
moment('2010-10-31').isBefore('2010-12-31', 'day');
# true
 
moment('2010-10-20').isBefore('2010-12-31', 'year');
# false
 
moment('2010-10-20').isAfter('2009-12-31', 'year'); 
# true
 
moment('2010-10-20').isSame('2009-12-31', 'year'); 
# 判断两个时间是否相等
 
# 需要注意的是, isBefore与isAfter 都是开区间,如果想使用闭区间,应使用
isSameOrBefore
isSameOrAfter

两个时间的相差几天

moment([2008, 2, 27]).diff([2007, 0, 28], 'day');
# 424

获取 月份和星期 枚举列表

moment.months()
 
# ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
 
moment.monthsShort()
# ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
 
moment.weekdays()
# ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
 
moment.weekdaysMin()
# ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]

是否是闰年

moment().isLeapYear();
# true
 
moment([2001]).isLeapYear() 
# false

moment 用法展示

import moment from 'moment';

const date = new Date;

console.log('按照format中的格式显示日期');
console.log('moment(date).format(YYYY-MM-DD HH:mm:ss)', moment(date).format('YYYY-MM-DD HH:mm:ss'));
console.log('');

console.log('按照format中的格式显示日期,其中formate中的参数可以更改为固定值');
console.log('moment(date).format(YYYY-MM-01 00:00:00)', moment(date).format('YYYY-MM-01 00:00:00'));
console.log('');

console.log('日期所在周的周日');
console.log('moment(date).endOf(week)', moment(date).endOf('week').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期所在周的周一');
console.log('moment(date).startOf(week)', moment(date).startOf('week').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的小时数');
console.log('moment(date).hour()', moment(date).hour());
console.log('');

console.log('日期的月份数(从0开始到11)');
console.log('moment(date).month()', moment(date).month());
console.log('');

console.log('日期的所在周是第几周');
console.log('moment(date).week()', moment(date).week());
console.log('');

console.log('日期的星期数');
console.log('moment(date).day()', moment(date).day());
console.log('');

console.log('日期的日数');
console.log('moment(date).date()', moment(date).date());
console.log('');

console.log('日期的六天前');
console.log('moment(date).add(-6, days)', moment(date).add(-6, 'days').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期所在月的最后一天');
console.log('moment(date).endOf(month)', moment(date).endOf('month').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的下一天');
console.log('moment(date).add(1, days)', moment(date).add(1, 'days').format('YYYY-MM-DD 00:00:00'));
console.log('');

console.log('日期的中国标准时间输出格式');
console.log('moment(date)._d', moment(date)._d);
console.log('');

console.log('日期所在月有多少天');
console.log('moment(date).daysInMonth()', moment(date).daysInMonth());
console.log('');

输出结果查看

image.png

image.png