日常开发经常会用Moment.js来处理时间,现对频繁使用的几个API做下整理,以便日后查阅
安装
npm install moment
获取时间
moment().valueOf()
moment().startOf('day')
moment().startOf('isoWeek')
moment().startOf('month')
moment().endOf('day')
moment().endOf('isoWeek')
moment().endOf('month')
moment().year()
moment().month()
moment().date()
moment().day()
moment().daysInMonth()
moment().month(moment().month() - 1).startOf('month').valueOf()
moment().month(moment().month() - 1).endOf('month').valueOf()
moment().month(moment().month() - 1).startOf('month').valueOf()
moment().month(moment().month() - 1).endOf('month').valueOf()
moment().add(4, 'years').format('YYYY-MM-DD');
moment().add(4, 'months').format('YYYY-MM-DD');
moment().add(4, 'days').format('YYYY-MM-DD');
moment([2016]).add(4, 'years').format('YYYY');
moment().subtract(10, 'years').format('YYYY');
moment().startOf('hour').format('YYYY-MM-DD HH:mm:ss');
moment().endOf('hour').format('YYYY-MM-DD HH:mm:ss');
格式化时间
moment().format('YYYY-MM-DD')
moment().format('HH:mm:ss')
moment().format('hh:mm:ss a')
moment().format('x')
moment().format('X')
moment().format();
moment().format('YYYY');
moment().format('Q');
moment().format('MMMM');
moment().format('DD');
moment().format('DDD');
moment().format('dddd');
moment().format('YYYY-MM-DD HH:mm:ss');
moment().format('YYYY[测]MM[试]DD HH[时]mm[间]ss');
moment().format('YYYY [test] YYYY');
moment().format('YYYY [test] MM');
moment().format('YYYY [test] DD');
转换JS原声Date对象
moment().toDate()
new Date(moment())
设置时间
moment().year(2019)
moment().month(9)
moment().date(2)
moment().isoWeekday(1)
moment().add(1, 'years')
moment().add(1, 'months')
moment().add(1, 'days')
moment().add(1, 'weeks')
moment().subtract(1, 'years')
moment().subtract(1, 'months')
moment().subtract(1, 'days')
moment().subtract(1, 'weeks')
比较时间
let startDate = moment().subtract(1, 'weeks')
let endDate = moment()
endDate.diff(startDate)
endDate.diff(startDate, 'months')
endDate.diff(startDate, 'weeks')
endDate.diff(startDate, 'days')
startDate.diff(endDate, 'days')
let startTime = moment(createTime).format('YYYY-MM-DD HH:mm:ss')
let endTime = moment().format('YYYY-MM-DD HH:mm:ss')
let start_date = moment(startTime, "YYYY-MM-DD HH:mm:ss");
let end_date = moment(endTime, "YYYY-MM-DD HH:mm:ss");
let seconds = end_date.diff(start_date, "seconds");
let mintus = (seconds / 60);
let hours = (mintus / 60);
let day = (hours/24)
时间戳字符串互相转换
moment(1259186487000).format('YYYY-MM-DD HH:mm:ss');
moment('2009-11-26 06:01:27').valueOf();
moment('2009-11-26 06:01:27');
相对时间
moment('20091126', 'YYYYMMDD').fromNow();
moment('20091126', 'YYYYMMDD').fromNow(true);
moment([2009, 11, 26]).fromNow();
moment().startOf('day').fromNow();
moment().endOf('hour').fromNow();
时间查询
moment([2009]).isLeapYear();
moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28');
moment('2009-11-26 06:01:27').isBefore('2009-11-26 06:01:28', 'year');
moment('2009-11-26').isAfter('2020-11-26', 'year');
moment('2016-11-26').isBetween('2009-11-26', '2020-11-26');
moment('2009-11', 'YYYY-MM').daysInMonth();
moment().weeksInYear();
时长创建
moment.duration(1500);
moment.duration(1500).milliseconds();
moment.duration(1500).seconds();
moment.duration(1500).asSeconds();
moment.duration(2, 'seconds');
moment.duration(2, 'years');
日历时间显示
moment.duration(1500);
moment.duration(1500).milliseconds();
moment.duration(1500).seconds();
moment.duration(1500).asSeconds();
moment.duration(2, 'seconds');
moment.duration(2, 'years');
切换中文
moment.defineLocale('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分',
LTS : 'Ah点m分s秒',
L : 'YYYY-MM-DD',
LL : 'YYYY年MMMD日',
LLL : 'YYYY年MMMD日Ah点mm分',
LLLL : 'YYYY年MMMD日ddddAh点mm分',
l : 'YYYY-MM-DD',
ll : 'YYYY年MMMD日',
lll : 'YYYY年MMMD日Ah点mm分',
llll : 'YYYY年MMMD日ddddAh点mm分'
},
meridiemParse: /凌晨|早上|上午|中午|下午|晚上/,
meridiemHour: function (hour, meridiem) {
if (hour === 12) {
hour = 0;
}
if (meridiem === '凌晨' || meridiem === '早上' ||
meridiem === '上午') {
return hour;
} else if (meridiem === '下午' || meridiem === '晚上') {
return hour + 12;
} else {
return hour >= 11 ? hour : hour + 12;
}
},
meridiem : function (hour, minute, isLower) {
var hm = hour * 100 + minute;
if (hm < 600) {
return '凌晨';
} else if (hm < 900) {
return '早上';
} else if (hm < 1130) {
return '上午';
} else if (hm < 1230) {
return '中午';
} else if (hm < 1800) {
return '下午';
} else {
return '晚上';
}
},
calendar : {
sameDay : function () {
return this.minutes() === 0 ? '[今天]Ah[点整]' : '[今天]LT';
},
nextDay : function () {
return this.minutes() === 0 ? '[明天]Ah[点整]' : '[明天]LT';
},
lastDay : function () {
return this.minutes() === 0 ? '[昨天]Ah[点整]' : '[昨天]LT';
},
nextWeek : function () {
var startOfWeek, prefix;
startOfWeek = moment().startOf('week');
prefix = this.unix() - startOfWeek.unix() >= 7 * 24 * 3600 ? '[下]' : '[本]';
return this.minutes() === 0 ? prefix + 'dddAh点整' : prefix + 'dddAh点mm';
},
lastWeek : function () {
var startOfWeek, prefix;
startOfWeek = moment().startOf('week');
prefix = this.unix() < startOfWeek.unix() ? '[上]' : '[本]';
return this.minutes() === 0 ? prefix + 'dddAh点整' : prefix + 'dddAh点mm';
},
sameElse : 'LL'
},
ordinalParse: /\d{1,2}(日|月|周)/,
ordinal : function (number, period) {
switch (period) {
case 'd':
case 'D':
case 'DDD':
return number + '日';
case 'M':
return number + '月';
case 'w':
case 'W':
return number + '周';
default:
return number;
}
},
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 年'
},
week : {
dow : 1,
doy : 4
}
});
秒换算日期
function formatSecToStr(seconds){
let daySec = 24 * 60 * 60;
let hourSec= 60 * 60;
let minuteSec=60;
let dd = Math.floor(seconds / daySec);
let hh = Math.floor((seconds % daySec) / hourSec);
let mm = Math.floor((seconds % hourSec) / minuteSec);
let ss=seconds%minuteSec;
if(dd > 0){
return dd + "天" + hh + "小时" + mm + "分钟"+ss+"秒";
}else if(hh > 0){
return hh + "小时" + mm + "分钟"+ss+"秒";
} else if (mm > 0){
return mm + "分钟"+ss+"秒";
}else{
return ss+"秒";
}
}