Vue.js 获取昨天、前天、一周前、一个月前时间戳

本文将介绍在 Vue.js 中获取不同时间段(昨天、前天、一周前、一个月前)的时间戳方法。

1. 获取昨天的时间戳

const yesterday = Date.now() - 24 * 60 * 60 * 1000;

2. 获取前天的时间戳

const dayBeforeYesterday = Date.now() - 2 * 24 * 60 * 60 * 1000;

3. 获取一周前的时间戳

const oneWeekAgo = Date.now() - 7 * 24 * 60 * 60 * 1000;

4. 获取一个月前的时间戳

const oneMonthAgo = Date.now() - 30 * 24 * 60 * 60 * 1000;

解释

  • Date.now() 获取当前时间戳(以毫秒为单位)。
  • 通过减去对应时间段的毫秒数,即可得到目标时间戳。

示例

<template>
  <div>
    <p>昨天的时间戳:{{ yesterday }}</p>
    <p>前天的时间戳:{{ dayBeforeYesterday }}</p>
    <p>一周前的时间戳:{{ oneWeekAgo }}</p>
    <p>一个月前的时间戳:{{ oneMonthAgo }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      yesterday: Date.now() - 24 * 60 * 60 * 1000,
      dayBeforeYesterday: Date.now() - 2 * 24 * 60 * 60 * 1000,
      oneWeekAgo: Date.now() - 7 * 24 * 60 * 60 * 1000,
      oneMonthAgo: Date.now() - 30 * 24 * 60 * 60 * 1000,
    };
  },
};
</script>

注意

  • 以上代码示例使用的是 Vue.js 模板语法,可以根据实际情况进行调整。
  • 获取一个月前的时间戳,假设一个月有 30 天,实际可能会有偏差。
  • 可以根据需要调整代码中的时间段值,获取其他时间戳。
Vue.js 获取昨天、前天、一周前、一个月前时间戳

原文地址: https://www.cveoy.top/t/topic/mj6j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录