Vue如何获取new Date().getTime()时间戳

 更新时间:2024年10月16日 08:48:07   作者:qianer0_0  
在Web开发中,前端使用Vue.js获取的是毫秒级时间戳,而PHP后端则是秒级时间戳,处理此类问题时,需要将PHP的时间戳乘以1000转换为毫秒级,以保证数据的一致性和正确的逻辑判断

vue获取new Date().getTime() 时间戳

在处理按钮显示的时候发现一个问题:

vue 通过new Date().getTime()获取时间戳返回的是13位数字,单位是毫秒;

php后台time()获取的时间戳是10位数字,单位秒;

所以在判断比较时需要将time()*1000 转换为毫秒再去比较

<el-button v-if="new Date(scope.row.end_time*1000).getTime()>new Date().getTime()"  size="mini" icon="edit" @click="editGroupsAction(scope.$index, scope.row)">编辑</el-button>

vue动态获取当前时间

获取当前时间:

<template>
  <div id="home">
    <span class="deadline">截止{{ gettime }}</span>
  </div>
</template>
 
<script>
export default {
  name: "Home",
  data() {
    return {
      gettime: "", //当前时间
    };
  },
 
  methods: {
    getTime() {
    var _this = this;
      let yy = new Date().getFullYear();
      var mm =
        new Date().getMonth() > 9
          ? new Date().getMonth() + 1
          : new Date().getMonth() == 9
          ? new Date().getMonth() + 1
          : '0' + (new Date().getMonth() + 1);
      var dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
    },
    currentTime() {
      setInterval(this.getTime, 1000);
    },
   
  },
  mounted() {
    this.currentTime();
  },
};
</script>
 
<style scoped>
</style>

获取当前日期:

<template>
  <div id="home">
    <span class="deadline">当前日期{{ sendTime }}</span>
  </div>
</template>
 
<script>
export default {
  name: "Home",
  data() {
    return {
      sendTime: "", //当前时间
    };
  },
  mounted() {
    this.format();
  },
  methods: {
      format() {
                    const nowDate = new Date();
                    const date = {
                        year: nowDate.getFullYear(),
                        month: nowDate.getMonth() + 1,
                        date: nowDate.getDate(),
                    }
                    const newmonth = date.month > 9 ? date.month : '0' + date.month
                    const day = date.date > 9 ? date.date : '0' + date.date
                    this.sendTime = date.year + '.' + newmonth + '.' + day
 
                }, //获取当前时间
   
  },
};
</script>
 
<style scoped>
</style>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • vue中let that=this的作用及说明

    vue中let that=this的作用及说明

    这篇文章主要介绍了vue中let that=this的作用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue实现简易计算器的4种方法举例

    vue实现简易计算器的4种方法举例

    这篇文章主要给大家介绍了关于vue实现简易计算器的4种方法,文中通过代码介绍的非常详细,对大家学习或者使用vue觉有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09
  • vue计算属性computed--getter和setter用法

    vue计算属性computed--getter和setter用法

    这篇文章主要介绍了vue计算属性computed--getter和setter用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Vue实现Google第三方登录的示例代码

    Vue实现Google第三方登录的示例代码

    本文记录作者在vue项目中使用到Google第三方登录,查询到的资料文档也不详细,故此把自己所遇到的坑及问题详细的记录下来。
    2021-07-07
  • vue中格式化时间过滤器代码实例

    vue中格式化时间过滤器代码实例

    这篇文章主要介绍了vue格式化时间过滤器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 在Vue组件中使用 TypeScript的方法

    在Vue组件中使用 TypeScript的方法

    typescript不仅可以约束我们的编码习惯,还能起到注释的作用,当我们看到一函数后我们立马就能知道这个函数的用法。这篇文章主要介绍了在Vue组件中使用 TypeScript的方法,需要的朋友可以参考下
    2018-02-02
  • Vue 集成 storybook的方法

    Vue 集成 storybook的方法

    这篇文章主要介绍了Vue 集成 storybook的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Vue组件封装之input输入框实战记录

    Vue组件封装之input输入框实战记录

    在vue中会将常用的组件进行封装,下面这篇文章主要给大家介绍了关于Vue组件封装之input输入框的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • vue路由传参方式的方式总结及获取参数详解

    vue路由传参方式的方式总结及获取参数详解

    vue 路由传参的使用场景一般都是应用在父路由跳转到子路由时,携带参数跳转,下面这篇文章主要给大家介绍了关于vue路由传参方式的方式总结及获取参数的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • uni-app中app和webview的h5通信简单步骤

    uni-app中app和webview的h5通信简单步骤

    这篇文章主要介绍了如何在nvue页面中使用webview组件,并详细介绍了如何在h5项目中安装和配置npmiy_uniwebview插件,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-02-02

最新评论