dandanyajin
今天更新博客了吗?

关于时间

获取当前时间:

new Date()                           //  Fri Jan 22 2021 14:40:45 GMT+0800 (中国标准时间)

new Date().getFullYear()     //  2021

new Date().getMonth()        //  0        (获取月时记得 +1 )

new Date().getDate()          //  22

new Date().getHours()        //  14


new Date().getMinutes()      //  43

new Date().getSeconds()     //   39

new Date().getMilliseconds()  //  217     (毫秒)

new Date().getDay()              //  5        (星期)    

new Date().getTimezoneOffset()    //  -480        与格林威治时间的时间差  (单位:分钟)

 

其他时间形式 转换为 时间戳

new Date().getTime()           //  1611297667687

 

  时间戳  转换成 其他时间形式 

   插件   dayjs:  https://dayjs.fenxianglu.cn/

 

方法:

 dateFilter(time, type) {
      let date = new Date(time * 1000)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      let hours = date.getHours()
      let minutes = date.getMinutes()
      let second = date.getSeconds()
      let format = (value) => {
        return value >= 10 ? value : '0' + value
      }
      let result
      switch (type) {
        case 0: // 01-05
          result = `${format(month)}月${format(day)}日`
          break
        case 1: // 11:12
          result = `${format(hours)}-${format(minutes)}`
          break
        case 2: // 2015-01-05
          result = `${year}-${format(month)}-${format(day)}`
          break
        case 3: // 2015-01-05 11:12
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours

 )}:${format(minutes)}`
          break
        case 4: // 2015.01.05 11:12
          result = `${year}.${month}.${day}  ${hours}:${minutes}`
          break
        case 5:
          result = `${year}年${format(month)}月${format(day)}日${format(
            hours

 )}时${format(minutes)}分`
        case 6: // 2015-01-05 11:12:06
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours
          )}:${format(minutes)}:${format(second)}`
          break
        case 7:
          result = `${year}-${format(month)}`
          break
        case 8:
          result = `${year}年${format(month)}月`
          break
        case 9: // 2015-01-05 11:12:06:32
          result = `${year}-${format(month)}-${format(day)}  ${format(
            hours
          )}:${format(minutes)}:${format(second)}`
          break
      }
      return result
    },

 

 

this.date = this.parseTime(new Date(),"{y}年{m}月{d}日");
this.week = this.parseTime(new Date(),"星期{a}");
this.time = this.parseTime(new Date(),"{h}:{i}:{s}");

 

/**
 * 格式化日期
 * @param {*} time 
 * @param {*} cFormat 
 * @returns 
 * @description '{y}-{m}-{d} {h}:{i}:{s}'
 */
function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  var date = void 0;
  if ((typeof time === 'undefined' ? 'undefined' : _typeof(time)) === 'object') {
    date = time;
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  var formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, function (result, key) {
    var value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value];
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value;
    }
    return value || 0;
  });
  return time_str;
}

 

posted on 2021-01-22 15:39  dandanyajin  阅读(141)  评论(0编辑  收藏  举报