/* * @Author: your name * @Date: 2021-04-19 10:00:37 * @LastEditTime: 2021-04-21 11:15:14 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \exclusive-cloud-admin\common\src\assets\js\timeCommon.ts */ const moment = require('../moment/moment.js'); function getAllTime(serverTime?: any) { const time = new Date(serverTime || new Date()); const myDate:any = {}; const year = time.getFullYear(), hour = moment().subtract('hours', 4), // getMonth return 0-11, express 1 - 12, so the month is equal to getMonth() plus one month = time.getMonth() + 1, // getMonth return 0-11, express 1 - 12, so the month is equal to getMonth() plus one day = time.getDate(), week = time.getDay() > 0 ? time.getDay() : 7, hours = time.getHours(), minutes = time.getMinutes(), seconds = time.getSeconds(), today = formatterDate(year, month, day, hours, minutes, seconds), thisWeek = calculateAgoDate(time, -(week - 1)), lastWeekBegin = calculateAgoDate(time, -(week - 1 + 7)), lastWeekEnd = calculateAgoDate(time, -(week)), yesterday = calculateAgoDate(time, -1), theDayBeforeYesterday = calculateAgoDate(time, -2), theMonthBeforeLastMonth = calculateAgoDate(time, -2, 'months'), theYearBeforeLastYear = calculateAgoDate(time, -2, 'years'), lastMonth = calculateAgoDate(time, -1, 'months'), nearlyFourHours = calculateAgoDate(time, -4, 'hours'), nearlyDay = calculateAgoDate(time, -1), nearlyWeek = calculateAgoDate(time, -7), nearlyMonth = calculateAgoDate(time, -1, 'months'), nearlyTwelveMonth = calculateAgoDate(time, -11, 'months'), // natural month // nearlyTwoMonth = calculateAgoDate(time, -2, 'months'), // natural month // 从当月开始算起,近两月为当月以及上月,近半年同理,例如 2018-04-01 00:00:00的近两月为 2018-03-01 00:00:00到2018-04-01 00:00:00 // 近半年为 2018-11-01 00:00:00到2018-04-01 00:00:00 nearlyHalfYear = calculateAgoDate(time, -5, 'months'), nearlyYear = calculateAgoDate(time, -1, 'years'); myDate['theDayBeforeYesterday'] = { startTime: formatterDate(theDayBeforeYesterday.year, theDayBeforeYesterday.month, theDayBeforeYesterday.day, 0, 0, 0), endTime: formatterDate(theDayBeforeYesterday.year, theDayBeforeYesterday.month, theDayBeforeYesterday.day, 23, 59, 59) }; myDate['yesterday'] = { startTime: formatterDate(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0), endTime: formatterDate(yesterday.year, yesterday.month, yesterday.day, 23, 59, 59) }; myDate['hour'] = { startTime: moment().subtract('hours', 4).format('YYYY-MM-DD HH:mm:ss'), endTime: moment(time) .subtract('minutes', 0) .format('YYYY-MM-DD HH:mm:ss') }; myDate['today'] = { startTime: formatterDate(year, month, day, 0, 0, 0), endTime: today }; myDate['lastWeek'] = { startTime: formatterDate(lastWeekBegin.year, lastWeekBegin.month, lastWeekBegin.day, 0, 0, 0), endTime: formatterDate(lastWeekEnd.year, lastWeekEnd.month, lastWeekEnd.day, 23, 59, 59) }; myDate['thisWeek'] = { startTime: formatterDate(thisWeek.year, thisWeek.month, thisWeek.day, 0, 0, 0), endTime: today }; myDate['theMonthBeforeLastMonth'] = { startTime: formatterDate(theMonthBeforeLastMonth.year, theMonthBeforeLastMonth.month, 1, 0, 0, 0), endTime: formatterDate(theMonthBeforeLastMonth.year, theMonthBeforeLastMonth.month, getMonthMaxDay(theMonthBeforeLastMonth.year, theMonthBeforeLastMonth.month), 23, 59, 59) }; myDate['lastMonth'] = { startTime: formatterDate(lastMonth.year, lastMonth.month, 1, 0, 0, 0), endTime: formatterDate(lastMonth.year, lastMonth.month, getMonthMaxDay(lastMonth.year, lastMonth.month), 23, 59, 59) }; myDate['thisMonth'] = { startTime: formatterDate(year, month, 1, 0, 0, 0), endTime: today }; myDate['theYearBeforeLastYear'] = { startTime: formatterDate(year - 2, 1, 1, 0, 0, 0), endTime: formatterDate(year - 2, 12, 31, 23, 59, 59) }; myDate['lastYear'] = { startTime: formatterDate(year - 1, 1, 1, 0, 0, 0), endTime: formatterDate(year - 1, 12, 31, 23, 59, 59) }; myDate['thisYear'] = { startTime: formatterDate(year, 1, 1, 0, 0, 0), endTime: today }; myDate['nearlyFourHours'] = { startTime: nearlyFourHours.time, endTime: today }; myDate['nearlyDay'] = { startTime: nearlyDay.time, endTime: today }; myDate['nearlyWeek'] = { startTime: nearlyWeek.time, endTime: today }; // natural month myDate['nearlyTwoMonth'] = { startTime: myDate['lastMonth'].startTime, endTime: today }; myDate['nearlyMonth'] = { startTime: nearlyMonth.time, endTime: today }; // natural month myDate['nearlyHalfYear'] = { startTime: moment(nearlyHalfYear.time).startOf('month').format('YYYY-MM-DD HH:mm:ss'), endTime: today }; myDate['nearlyYear'] = { startTime: nearlyYear.time, endTime: today }; myDate['nearlyTwelveMonth'] = { startTime: moment(nearlyTwelveMonth.time).startOf('month').format('YYYY-MM-DD HH:mm:ss'), endTime: today }; return myDate; } /** * 根据类型获取时间段 * @param {[type or String]} num [the value you want to add zero] * @return {[obj]} */ function getTimeByType( type: string) { let timeObj = getAllTime(); return timeObj[type] || timeObj; } /** * add zero before the value * @param {[Number or String]} num [the value you want to add zero] * @return {[Number or String]} [the value had been add zero] */ function addZero(num: any) { return +num > 9 ? num : '0' + num; } /** * according to the number of days pushing back, calculate the date time * @param {[Date]} time [the time of today] * @param {[Number]} ago [The number of days pushing back] * @param {[String]} type ['days', 'months', or 'years', the date type you want to calculate, the type is come from comment.js] * @return {[Object]} [time object after calculation] */ function calculateAgoDate(time: any, ago: any, type: string = 'days') { let temp; // if (type === 'month') { // temp = new Date(time.setMonth(time.getMonth() - ago)); // time.setMonth(time.getMonth() + ago); // } else if (type === 'year') { // temp = new Date(time.setFullYear(time.getFullYear() - ago)); // time.setFullYear(time.getFullYear() + ago); // } else if (type === 'hour') { // temp = new Date(time.setHours(time.getHours() - ago)); // time.setHours(time.getHours() + ago); // } else { // temp = new Date(time.setDate(time.getDate() - ago)); // time.setDate(time.getDate() + ago); // } temp = new Date(moment(time).add(ago, type).format()); const year = temp.getFullYear(), month = temp.getMonth() + 1, // getMonth return 0-11, express 1 - 12, so the month is equal to getMonth() plus one day = temp.getDate(), hours = temp.getHours(), minutes = temp.getMinutes(), seconds = temp.getSeconds(), date = formatterDate(year, month, day, hours, minutes, seconds); return { time: date, year: year, month: month, day: day, hours: hours, minutes: minutes, seconds: seconds }; } /** * 获取当前月份的天数 * @param {[Number]} year [which year] * @param {[Number]} month [which month] * @return {[Number]} [the max day of month] */ function getMonthMaxDay(year:any, month:any) { return new Date(year, month, 0).getDate(); } /** * according to you params, return the date time * @param {[Number]} year [which year] * @param {[Number]} month [which month] * @param {[Number]} day [which day] * @param {[Number]} hour [which hour] * @param {[Number]} minute [which minute] * @param {[Number]} second [which second] * @return {[String]} [date time] */ function formatterDate(year: any, month: any, day: any, hour: any, minute: any, second: any, split: string = '-') { return year + split + addZero(month) + split + addZero(day) + ' ' + addZero(hour) + ':' + addZero(minute) + ':' + addZero(second); } export default{ getAllTime, formatterDate, getMonthMaxDay, calculateAgoDate, addZero, getTimeByType, }