时间戳转换
const nowTime = moment().format();
console.log('获取当前时间', nowTime);
const nowSecondTimestamp = moment(nowTime).unix();
console.log('当前时间转换为秒级时间戳', nowSecondTimestamp);
const nowMillisecondTimestamp = moment(nowTime).valueOf();
console.log('当前时间转换为毫秒级时间戳', nowMillisecondTimestamp);
年份操作
const thisYear = moment(nowTime).format('YYYY');
console.log('获取当前年份', thisYear);
const preYear = moment(nowTime).subtract(1, 'year').format('YYYY');
console.log('获取现在时间的上一个年份', preYear);
const targetPreYear = moment(preYear).subtract(1, 'year').format('YYYY');
console.log('获取指定年份的上一年', targetPreYear);
月份操作
const thisMonth = moment(nowTime).format('MM');
console.log('获取当前月份', thisMonth);
const preMonth = moment(nowTime).subtract(1, 'month').format('MM');
console.log('获取上个月份', preMonth);
const thisMonthDays = moment(nowTime).daysInMonth();
console.log('获取当天月份天数', thisMonthDays);
const preMonthDays = moment(nowTime).subtract(1, 'month').daysInMonth();
console.log('获取上个月份天数', preMonthDays);
const targetMonthDays = moment(preMonth).daysInMonth();
console.log('获取指定月份天数', targetMonthDays);
const thisMonthFirstDays = moment(nowTime).startOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天', thisMonthFirstDays);
const thisMonthEndDays = moment(nowTime).endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('当月最后一天', thisMonthEndDays);
const thisMonthFirstDaysBeforce7Days = moment(thisMonthFirstDays).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天前七天', thisMonthFirstDaysBeforce7Days);
const thisMonthEndDaysBeforce7Days = moment(thisMonthEndDays).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月月底前七天', thisMonthEndDaysBeforce7Days);
const thisMonthFirstDaysAfter7Days = moment(thisMonthFirstDays).subtract(-7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第一天后七天', thisMonthFirstDaysAfter7Days);
const thisMonthEndDaysAfter7Days = moment(thisMonthEndDays).subtract(-7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('当月月底后七天', thisMonthEndDaysAfter7Days);
const preMonthSecondDay = moment(nowTime).subtract(1, 'month').startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('上个月第二周周一', preMonthSecondDay);
const thisMonthSecondDay = moment(nowTime).startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('当月第二周周一', thisMonthSecondDay);
const nextMonthSecondDay = moment(nowTime).subtract(-1, 'month').startOf('month').subtract(-2, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('下个月第二周周一', nextMonthSecondDay);
const targetMonthStartTime = moment('2021-02').startOf('month').format('YYYY-MM-DD HH:mm:ss');
const targetMonthEndTime = moment('2021-02').endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('指定月份的开始和结束时间', targetMonthStartTime, targetMonthEndTime);
const monthDiff = moment(nowTime).diff('2021-01', 'month', true);
console.log('比较两个时间相差月份', monthDiff);
const nextMonthFirstDay = moment(nowTime).add(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('获取下个月第一天', nextMonthFirstDay);
const nextMonthLastDay = moment(nowTime).add(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('获取下个月最后一天', nextMonthLastDay);
星期操作
const thisDateWeek = moment(nowTime).day();
console.log('获取当前时间是周几 星期', thisDateWeek);
const thisMonthFirstWeekStartTime = moment(nowTime).startOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss');
const thisMonthFirstWeekEndTime = moment(nowTime).endOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('本周开始和结束时间', thisMonthFirstWeekStartTime, thisMonthFirstWeekEndTime);
const thisMonthFirstMonthStartTime = moment(nowTime).startOf('month').format('YYYY-MM-DD HH:mm:ss');
const thisMonthFirstMonthEndTime = moment(nowTime).endOf('month').format('YYYY-MM-DD HH:mm:ss');
console.log('本月开始和结束时间', thisMonthFirstMonthStartTime, thisMonthFirstMonthEndTime);
// 比较两个时间相差星期
const weekDiff = moment(nowTime).diff('2021-01', 'week', true);
console.log('比较相差的星期', weekDiff);
const preeWeek = moment(nowTime).subtract(1, 'week').format('YYYY-MM-DD HH:mm:ss');
console.log('一周前的时间', preeWeek);
// 在这里需要加2天 是因为 moment的星期天是从0开始
const preeTuesday = moment(nowTime).subtract(1, 'week').startOf('week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('上周二', preeTuesday);
const thisTuesday = moment(nowTime).startOf('week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('本周二', thisTuesday);
const nextTuesday = moment(nowTime).startOf('week').add(1, 'week').add(2, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('下周二', nextTuesday);
指定天操作
// 比较两个时间相差的天数
const dayDiff = moment(nowTime).diff('2021-01', 'day', true);
console.log('比较相差的天数', dayDiff);
const beforeTenDays = moment(nowTime).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss');
console.log('查看七天前的日期', beforeTenDays);
// 时分秒操作
const isBeforeDate = moment('2021-02-01').isBefore('2021-01-01');
console.log('是否在指定时间之前', isBeforeDate);
const isAfter = moment('2021-02-01').isAfter('2021-01-01');
console.log('是否在指定时间之后', isAfter);
const isSame = moment('2021-02-01').isSame('2021-02-01');
console.log('是否相同时间', isSame);
// 时间校验
console.log('校验是否争取的时间格式', moment('202112312asdxzsc').isValid());
moment.js 时区转换
根据本地时区 将传入的中国时间戳转换为标准的格林威治时间戳
// 将传入的中国时间戳 转换为格林威治时间戳
transferTimeStamp(time){
// 中国时区
const chinaOffset = 8;
// 目标时间 同一个时间在不同地区中的时间戳不同
const defaultTime = time * 1000; //1626364800 2021/7/16 00:00:00
// 获取指定时间戳和格林威治时间相差的毫秒数
const localOffset = new Date(defaultTime).getTimezoneOffset() * 60000;
// 得到完整的格林威治时间
const utc = defaultTime + localOffset;
// 获取指定时间戳和格林威治时间差 小时
const offset = Math.abs(localOffset / 1000) / (60 * 60);
// 获取中国时间和本地时间的时间差
let diff = offset - chinaOffset;
// 减去和中国时区相差的时间(同一个时间在不同地区中的时间戳不同) 得到最终的本地时间戳
const localTime = utc + 3600000 * offset - 3600000 * diff;
return localTime / 1000;
}
根据本地时区 将传入的YYYY-MM-DD时间转换为标准的格林威治时间戳
// 将传入的YYYY-MM-DD时间转换为格林威治时间
transferTime(time){
// 目标时间
const defaultTime = time;
// 本地指定时间时间戳
const defaultNumberTime = new Date(defaultTime).getTime();
// 获取本地时间和格林威治时间相差的毫秒数
let localOffset = new Date().getTimezoneOffset() * 60000;
localOffset = localOffset > 0 ? -localOffset : localOffset;
// 得到完整的格林威治时间
const utc = defaultNumberTime + localOffset;
// 获取本地时间和格林威治时间差 小时
const offset = Math.abs(localOffset / 1000) / (60 * 60);
const localTime = utc + 3600000 * offset;
return localTime / 1000;
}
根据本地时区 将传入的中国时间戳转换为标准的YYYY-MM-DD时间
// 转换目标时间 得到指定时间格式
transferTimeFormat(time){
// 目标时间
const defaultTime = time;
// 本地指定时间时间戳
const defaultNumberTime = new Date(defaultTime).getTime();
// 获取本地时间和格林威治时间相差的毫秒数
let localOffset = new Date().getTimezoneOffset() * 60000;
localOffset = localOffset > 0 ? -localOffset : localOffset;
// 得到完整的格林威治时间
const utc = defaultNumberTime + localOffset;
// 获取本地时间和格林威治时间差 小时
const offset = Math.abs(localOffset / 1000) / (60 * 60);
const localTime = utc + 3600000 * offset;
const localTimeFormat = moment(localTime).format('YYYY-MM-DD');
return localTimeFormat;
}