vue中根据时间戳判断对应的时间(今天 昨天 前天)

yizhihongxing

下面是针对“vue中根据时间戳判断对应的时间(今天 昨天 前天)”的完整攻略。

1. 时间戳转换为日期

要想根据时间戳判断对应的时间,我们首先需要将时间戳转换为日期。JavaScript提供了Date()对象用于操作日期和时间。我们可以使用Date()对象和getTime()方法来将时间戳转换成日期对象。

举个例子,如果我们有一个时间戳变量timestamp,可以通过下面的代码将其转换为日期变量date

let timestamp = 1629849065000; // 时间戳
let date = new Date(timestamp); // 时间戳转日期

2. 将日期格式化为特定格式的字符串

将日期格式化为特定格式的字符串有很多种方法,可以使用原生的JavaScript方法,也可以使用开源库如moment.js。我们在这里介绍一种比较简单的方法,使用Date()对象的getYear()、getMonth()、getDay()、getHours()、getMinutes()和getSeconds()方法来获取年、月、日、时、分和秒数。然后,我们拼接这些信息来生成一个特定格式的字符串。

举个例子,以下代码根据现在的日期生成一个格式为“年-月-日 时:分:秒”的字符串:

let now = new Date();
let year = now.getYear() + 1900;
let month = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
let day = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
let hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
let minute = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
let second = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
let dateStr = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

3. 判断对应的时间

有了日期变量,接下来我们就可以根据时间戳判断对应的时间了。我们假设现在有一个时间戳变量timestamp,以下是判断对应的时间的实现过程:

let timestamp = new Date().getTime(); // 获取当前时间戳
let now = new Date(); // 获取当前日期

// 获取和比较时间差
let diff = now.getTime() - timestamp;
let diffDays = Math.floor(diff / (24 * 60 * 60 * 1000));

// 根据时间差返回合适的时间文本
if (diffDays === 0) {
  // 在同一天
  let hour = timestamp.getHours() < 10 ? "0" + timestamp.getHours() : timestamp.getHours();
  let minute = timestamp.getMinutes() < 10 ? "0" + timestamp.getMinutes() : timestamp.getMinutes();
  let timeStr = hour + ":" + minute;
  return timeStr;
} else if (diffDays === 1) {
  // 昨天
  return "昨天";
} else if (diffDays === 2) {
  // 前天
  return "前天";
} else {
  // 更早时间,直接返回日期
  let year = timestamp.getFullYear();
  let month = timestamp.getMonth() < 9 ? "0" + (timestamp.getMonth() + 1) : timestamp.getMonth() + 1;
  let day = timestamp.getDate() < 10 ? "0" + timestamp.getDate() : timestamp.getDate();
  let dateStr = year + "-" + month + "-" + day;
  return dateStr;
}

4. 示例

示例1

以下是一个用Vue.js封装的时间组件,可以根据时间戳判断对应的时间:

<template>
  <div>
    {{ getTimeString(timestamp) }}
  </div>
</template>

<script>
export default {
  props: {
    timestamp: {
      type: Number,
      required: true
    }
  },
  methods: {
    getTimeString(timestamp) {
      let now = new Date(); // 获取当前日期

      // 获取和比较时间差
      let diff = now.getTime() - timestamp;
      let diffDays = Math.floor(diff / (24 * 60 * 60 * 1000));

      // 根据时间差返回合适的时间文本
      if (diffDays === 0) {
        // 在同一天
        let hour = timestamp.getHours() < 10 ? "0" + timestamp.getHours() : timestamp.getHours();
        let minute = timestamp.getMinutes() < 10 ? "0" + timestamp.getMinutes() : timestamp.getMinutes();
        let timeStr = hour + ":" + minute;
        return timeStr;
      } else if (diffDays === 1) {
        // 昨天
        return "昨天";
      } else if (diffDays === 2) {
        // 前天
        return "前天";
      } else {
        // 更早时间,直接返回日期
        let year = timestamp.getFullYear();
        let month = timestamp.getMonth() < 9 ? "0" + (timestamp.getMonth() + 1) : timestamp.getMonth() + 1;
        let day = timestamp.getDate() < 10 ? "0" + timestamp.getDate() : timestamp.getDate();
        let dateStr = year + "-" + month + "-" + day;
        return dateStr;
      }
    }
  }
};
</script>

示例2

以下是另一个用Vue.js封装的时间组件,在不同场景下可以显示不同的格式:

<template>
  <div>
    {{ getTimeString(timestamp, type) }}
  </div>
</template>

<script>
export default {
  props: {
    timestamp: {
      type: Number,
      required: true
    },
    type: {
      type: String,
      default: "default" // 可选值: default, short, full
    }
  },
  methods: {
    getTimeString(timestamp, type) {
      let now = new Date(); // 获取当前日期

      // 获取和比较时间差
      let diff = now.getTime() - timestamp;
      let diffDays = Math.floor(diff / (24 * 60 * 60 * 1000));

      // 根据时间差返回合适的时间文本
      if (type === "default") {
        // 默认格式,自动判断显示今天、昨天、前天或完整日期
        if (diffDays === 0) {
          // 在同一天
          let hour = timestamp.getHours() < 10 ? "0" + timestamp.getHours() : timestamp.getHours();
          let minute = timestamp.getMinutes() < 10 ? "0" + timestamp.getMinutes() : timestamp.getMinutes();
          let timeStr = hour + ":" + minute;
          return timeStr;
        } else if (diffDays === 1) {
          // 昨天
          return "昨天";
        } else if (diffDays === 2) {
          // 前天
          return "前天";
        } else {
          // 更早时间,直接返回日期
          let year = timestamp.getFullYear();
          let month = timestamp.getMonth() < 9 ? "0" + (timestamp.getMonth() + 1) : timestamp.getMonth() + 1;
          let day = timestamp.getDate() < 10 ? "0" + timestamp.getDate() : timestamp.getDate();
          let dateStr = year + "-" + month + "-" + day;
          return dateStr;
        }
      } else if (type === "short") {
        // 简短格式,只显示日期
        let year = timestamp.getFullYear();
        let month = timestamp.getMonth() < 9 ? "0" + (timestamp.getMonth() + 1) : timestamp.getMonth() + 1;
        let day = timestamp.getDate() < 10 ? "0" + timestamp.getDate() : timestamp.getDate();
        let dateStr = year + "-" + month + "-" + day;
        return dateStr;
      } else if (type === "full") {
        // 完整格式,显示日期和时间
        let year = timestamp.getFullYear();
        let month = timestamp.getMonth() < 9 ? "0" + (timestamp.getMonth() + 1) : timestamp.getMonth() + 1;
        let day = timestamp.getDate() < 10 ? "0" + timestamp.getDate() : timestamp.getDate();
        let hour = timestamp.getHours() < 10 ? "0" + timestamp.getHours() : timestamp.getHours();
        let minute = timestamp.getMinutes() < 10 ? "0" + timestamp.getMinutes() : timestamp.getMinutes();
        let second = timestamp.getSeconds() < 10 ? "0" + timestamp.getSeconds() : timestamp.getSeconds();
        let dateStr = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        return dateStr;
      }
    }
  }
};
</script>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue中根据时间戳判断对应的时间(今天 昨天 前天) - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • Springboot实现Shiro整合JWT的示例代码

    下面来详细讲解如何实现Spring Boot整合Shiro和JWT的示例代码。 简介 Shiro是一个强大的安全框架,提供了多种安全特性,例如:认证、授权、加密等等。JWT是一种轻量级的认证机制,它可以使用JSON格式存储用户信息,并且可以在客户端和服务端之间传递。 本文将介绍如何通过Spring Boot实现Shiro整合JWT的示例代码。 示例1:环境搭…

    Vue 2023年5月28日
    00
  • vue v-for循环出来的数据动态绑定值问题

    当使用Vue的v-for指令循环展示数据时,我们需要注意数据与状态的动态绑定问题。本文将详细讲解使用v-for指令循环展示数据时,因为数据改变而导致状态动态绑定的问题及解决方法。 问题现象 假设有如下一段v-for指令: <div v-for="item in items"> <input type="text…

    Vue 2023年5月29日
    00
  • 详解Vue前端生产环境发布配置实战篇

    下面我将为您详细讲解“详解Vue前端生产环境发布配置实战篇”的完整攻略,包含以下内容: 一、前置准备 在开始前,我们需要完成以下准备工作: 安装Node.js和Vue-cli 创建一个Vue项目 配置生产环境的基础设置 二、环境配置 修改“package.json”文件中的“build”脚本,指定“NODE_ENV”为“production”,示例如下: &…

    Vue 2023年5月28日
    00
  • 使用Vue逐步实现Watch属性详解

    使用Vue逐步实现Watch属性详解,需要以下几个步骤: 创建一个Vue实例,定义data属性。例如,创建一个input框和一个div标签,将input的值绑定到data中的message属性,将div标签的内容绑定到data中的output属性。 <div id="app"> <input v-model="…

    Vue 2023年5月27日
    00
  • Mpvue中使用Vant Weapp组件库的方法步骤

    使用Vant Weapp组件库的方法步骤: 安装Vant Weapp 在cmd中进入mpvue项目根目录,执行以下命令进行安装: npm install vant-weapp -S –production 在 App.vue 中引入 Vant Weapp 在 App.vue 的 script 标签中如下添加: import Vant from ‘vant-…

    Vue 2023年5月27日
    00
  • Vue 单文件中的数据传递示例

    下面是“Vue 单文件中的数据传递示例”的完整攻略。 什么是 Vue 单文件组件 Vue 单文件组件是一种使用 .vue 扩展名的文件,它包含了三部分内容: 模板(template):用于渲染 HTML 结构的模板。 脚本(script):包含了组件的逻辑和数据等。 样式(style):用于渲染样式的 CSS 代码。 单文件组件使得一个组件的所有内容都被封装…

    Vue 2023年5月28日
    00
  • Vue中methods的this指向问题浅析

    下面是详细讲解“Vue中methods的this指向问题浅析”的完整攻略。 1. 什么是Vue中的methods? 在Vue组件里,methods是用于存放方法的一个对象。它可以包含各种实例方法,例如事件监听、异步请求等等。在组件内部可以通过this关键字来调用methods里面的方法。 2. methods中的this指向问题 在Vue中,methods中…

    Vue 2023年5月28日
    00
  • echarts报错:Error in mounted hook的解决方法

    下面是关于”echarts报错:Error in mounted hook的解决方法”的完整攻略。 什么是“echarts报错:Error in mounted hook”的问题? 当使用 echarts 绘制图表时,有时在控制台会看到一个错误提示:“Error in mounted hook”,通常提示中还会包含一些错误信息,比如:“Cannot read…

    Vue 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部