阅读 101

封装日期格式化函数(备用)

日期格式化:

注意:传入时需要转换为字符串

function nowTime(mytime) {
    let time = new Date(mytime);
    // 年:
    let year = time.getFullYear();
    // 月
    let month = time.getMonth() + 1;
    month = month < 10 ? '0' + month : month;
    // 日
    let date = time.getDate();
    date = date < 10 ? '0' + date : date;
    // 时
    let hour = time.getHours();
    hour = hour < 10 ? '0' + hour : hour;
    // 分
    let minute = time.getMinutes();
    minute = minute < 10 ? '0' + minute : minute;
    // 秒
    let second = time.getSeconds();
    second = second < 10 ? '0' + second : second;

    let timeStr = `${year}年-${month}月-${date}日 ${hour}:${minute}:${second}`;

    return timeStr;
  }

console.log(nowTime('Wed Jan 01 2020 20:30:12 GMT+0800 (中国标准时间)'));
复制代码

目标时间倒计时:

 function getTime() {
            let time = new Date();
            // 年:
            let year = time.getFullYear();
            // 月
            let month = time.getMonth() + 1;
            month = month < 10 ? '0' + month : month;
            // 日
            let date = time.getDate();
            date = date < 10 ? '0' + date : date;
            // 时
            let hour = time.getHours();
            hour = hour < 10 ? '0' + hour : hour;
            // 分
            let minute = time.getMinutes();
            minute = minute < 10 ? '0' + minute : minute;
            // 秒
            let second = time.getSeconds();
            second = second < 10 ? '0' + second : second;

            let timeStr = `${year}年-${month}月-${date}日   ${hour}:${minute}:${second}`;

            return timeStr;
        }

        let a = getTime();
        // console.log(a);
        function countDown() {
            let b = document.getElementById('mydiv');
            let futureTime = +new Date('2021-7-31 21:00:00');
            let nowTime = +new Date();
            let res = futureTime - nowTime;
            res = res / 1000; // 转化为秒;

            let day = parseInt(res / 60 / 60 / 24);
            let hour = parseInt(res / 60 / 60 % 24);
            let minate = parseInt(res / 60 % 60);
            let second = parseInt(res % 60);
            b.innerText = `距离倒计时结束还有${day}天,${hour}小时,${minate}分钟,${second}秒`;
        }
        countDown(); // 先调用函数,防止有100ms的空白间隔;
        setInterval(countDown, 100);
复制代码

日期格式化2

参数一:时间戳,参数二日期格式

function parseTime (time, cFormat) {
  if (arguments.length === 0) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }

  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    date = new Date(parseInt(time))
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') {
      return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return timeStr
}
复制代码
文章分类
前端
文章标签