获取时间信息

/**
 * 获取时间信息
 */
// 时间字符串
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),
      }
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值