时间戳是什么?JS中时间戳的展示

1,什么是时间戳

一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。

说白了就是,表示某一刻的时间;

2,时间戳有什么用;

一般啊,在互联网公司都会在项目种使用时间戳,时间戳主要用于清理缓存大多数用于版本更新;

3,JS中的具体的时间戳功能的实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const date = new Date("1996-6-6 08:22:12");
      console.log(date * 1);
      console.log(Number(date));
      console.log(date.valueOf());
      console.log(date.getTime());

      const timestamp = date.valueOf();
      console.log(timestamp);
      console.log(new Date(timestamp));

      function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
        const config = {
          YYYY: date.getFullYear(),
          MM: date.getMonth() + 1,
          DD: date.getDate(),
          HH: date.getHours(),
          mm: date.getMinutes(),
          ss: date.getSeconds(),
        };

        for (const key in config) {
          console.log(key, config[key]);
          format = format.replace(key, config[key]);
        }
        return format;
      }
      console.log(dateFormat(date));
      //传入自己想要的格式
      console.log(dateFormat(date, 'YYYY年MM月DD日'));
    </script>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结果才重要

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值