时间戳格式化详细 (包括北京时间上午,中午,下午,晚上等,及星期几)

方法

function FormatTimeDetail (timestamp) {
  if (!isNaN(timestamp)) {
    const TimestampToTime = (timestamp) => {
      const date = new Date(timestamp); // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
      const Y = date.getFullYear();
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
      const D = date.getDate();
      const h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
      const m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      return Y + '年' + M + '月' + D + '日' + ' ' + h + ':' + m + ':' + s;
    };
    const now = new Date(timestamp); // 日期 
    const nowDayOfWeek = now.getDay(); // 周的第几天 
    const nowDayOfHours = now.getHours(); // 第几小时
    const weekFormat = {
      1: '星期一',
      2: '星期二',
      3: '星期三',
      4: '星期四',
      5: '星期五',
      6: '星期六',
      0: '星期日'
    };
    const date = TimestampToTime(timestamp);
    const week = weekFormat[nowDayOfWeek];
    let timeProgress = '';
    if (nowDayOfHours >= 1 && nowDayOfHours <= 5) {
      timeProgress = '北京时间凌晨';
    } else if (nowDayOfHours > 5 && nowDayOfHours <= 6) {
      timeProgress = '北京时间清晨';
    } else if (nowDayOfHours > 6 && nowDayOfHours <= 11) {
      timeProgress = '北京时间早上';
    } else if (nowDayOfHours > 11 && nowDayOfHours <= 13) {
      timeProgress = '北京时间中午';
    } else if (nowDayOfHours > 13 && nowDayOfHours <= 17) {
      timeProgress = '北京时间下午';
    } else if (nowDayOfHours > 17 && nowDayOfHours <= 18) {
      timeProgress = '北京时间傍晚';
    } else if (nowDayOfHours > 18 && nowDayOfHours <= 24) {
      timeProgress = '北京时间晚上';
    } else {
      timeProgress = '北京时间午夜';
    }
    const cdata = {
      'date': date,
      'week': week,
      'timeProgress': timeProgress
    };
    return cdata;
  } else {
    return '';
  }
};

调用及打印

eg:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值