/** * 获取时间信息 */ // 时间字符串 const weekStr = ['一', '二', '三', '四', '五', '六', '日'] // 十进制字符数组 const decimalSystemZh = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'] /** * 小余10大于等于0加'0' */ const addZero=(num)=>{ return num < 10 ? '0' + num : num } /** * 获取十进制汉字最大值为100以内 */ const getZhDecimalSystem=(num)=> { if (num > 10) { const numStr = num + '' const tenNum = parseInt(numStr[0]) const bitNum = parseInt(numStr[1]) const tenPlace = ((tenNum === 1) ? '' : decimalSystemZh[parseInt(tenNum - 1)]) + '十' const bit = (bitNum === 0) ? '' : decimalSystemZh[parseInt(numStr[1]) - 1] return tenPlace + bit } else { return decimalSystemZh[num - 1] } } export default class GetTime { /** * 获取当前时间或根据时间对象获取时间信息 * @param NewDate 任意时间对象 * @returns {{timeStamp: number, hours: number, seconds: number, mouth: number, week: number, year: number, minutes: number, millisecond: number, day: number, weekZh: string}} * @constructor */ static dateToTime(NewDate) { const date = NewDate || new Date() // 时间戳 const timeStamp = date.getTime() // 整年值 const year = date.getFullYear() // 月 const mouth = date.getMonth() + 1 // 星期几 const week = date.getDay() // 小时 const hours = date.getDay() // 分 const minutes = date.getMinutes() // 秒 const seconds = date.getSeconds() // 天 const day = date.getDate() // 毫秒 const millisecond = date.getMilliseconds() return { timeStamp, year, mouth, day, hours, minutes, seconds, millisecond, week, weekZh: weekStr[week - 1] } } /** * 获取时间对象所在月天数 * @param NewDate 任意时间对象 */ static getMonthDays(NewDate) { const {year,mouth}=this.dateToTime(NewDate); const monthStartDate = new Date(year,mouth, 1); const monthEndDate = new Date(year,mouth + 1, 1); return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24); } /** * 获取当前时间信息 * @param linkStr 年月日链接字符 * @param NewDate 时间对象 */ static timeToLink(linkStr,NewDate) { const { timeStamp, year, mouth, week, day, hours, minutes, seconds } = this.dateToTime(NewDate); // 链接字符 const link = linkStr || '-' // 显示年月日 const YYYYMMDD = year + link + addZero(mouth) + link + addZero(day) const newHours = addZero(hours) const newMinutes = addZero(minutes) const newSeconds = addZero(seconds) return { timeStamp, weekZh: '星期' + weekStr[week - 1], YYYYMMDD, YYYYMMDDHH: YYYYMMDD + ' ' + newHours, YYYYMMDDHHMM: YYYYMMDD + ' ' + newHours + ':' + newMinutes, YYYYMMDDHHMMSS: YYYYMMDD + ' ' + newHours + ':' + newMinutes + ':' + newSeconds, days:this.getMonthDays(NewDate) } } /** * 获取时间中文表达值 * @param NewDate 时间对象 */ static getZhTimeInfo(NewDate) { const { year, mouth, day } = this.dateToTime(NewDate) const yearStr = year + '' const len = yearStr.length const yearNum = parseInt(yearStr.substr(len - 2)) return { yearZh: getZhDecimalSystem(yearNum), mouthZh: getZhDecimalSystem(mouth), dayZh: getZhDecimalSystem(day), } } }
获取时间信息
最新推荐文章于 2021-07-04 15:13:48 发布