【vue里面的时间转换】

最近遇到一个需求,涉及到一系列的时间转换,包括由年月日时分秒减去分钟,时间戳转换为YYYY-MM-DD HH:mm:ss,时间做减法转为分钟

具体如下

时间减分钟

let beforeOneHour = new Date( new Date(具体选择的时间).getTime() - 分钟数 * 60 * 1000 );

beforeOneHour算出来是一个gmt的时间戳格式

接下来写一个方法来处理时间戳格式数据

 // 返回格式化时间

    timestampToTime(timestamp) {

   // 需要时间转为毫秒

    timestamp=timestamp.getTime();

     // 时间戳为10位需*1000,时间戳为13位不需乘1000,这里的时间不需要乘1000

      var date = new Date(timestamp);

      var Y = date.getFullYear() + "-";

      var M =(date.getMonth() + 1 < 10  ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";

      var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";

      var h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";

      var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";

      var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

      // 返回格式化时间

      return Y + M + D + h + m + s;

    },

最后就是时间减时间,算时间差距多少分钟

js计算时间是需要转化为毫秒来计算的所以我们要把时间格式化

let tattime = new Date(时间1).getTime() -  new Date(时间二).getTime();

这样算出来的就是一个毫秒,最后再把  tattime/60000 算出来的就是两者之间差几分钟

           

           

     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,可以看出这是一个Vue模板中的时间转换问题。具体来说,引用中演示了如何将年月日时分秒格式的时间转换时间戳,而引用中演示了如何将年月日时分秒格式的时间转换为标准时间格式。下面是一个Vue模板中的时间转换的例子: ```html <template> <div> <h3>时间转换</h3> <p>时间转换为标准时间格式:</p> <p>{{ timestampToTime(1629190800000) }}</p> <p>标准时间格式转换时间戳:</p> <p>{{ timeToTimestamp('2021-08-17 09:00:00') }}</p> </div> </template> <script> export default { methods: { // 时间转换为标准时间格式 timestampToTime(timestamp) { const date = new Date(timestamp) const Y = date.getFullYear() + '-' const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-' const D = (date.getDate() < 10 ? '0' + (date.getDate()) : 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 }, // 标准时间格式转换时间戳 timeToTimestamp(time) { return Date.parse(new Date(time)) } } } </script> ``` 在上面的例子中,我们定义了两个方法:`timestampToTime`和`timeToTimestamp`。`timestampToTime`方法将时间转换为标准时间格式,而`timeToTimestamp`方法将标准时间格式转换时间戳。我们可以在Vue模板中使用这两个方法来进行时间转换

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值