vue封装-获取当前时间之中国标准时间

vue封装-获取当前时间之中国标准时间

1、方法封装

src/utils/time.js

/*
 * @Author: admin
 * @Date: 2022-02-26 09:30:48
 * @LastEditTime: 2022-02-26 09:30:48
 * @LastEditors: Please set LastEditors
 * @Description: 时间戳转换 --> 2022-02-26 09:30:48
 * @FilePath: src/utils/time.js
 */
class Time {
  // 1645839048000 --> 2022-02-26 09:30:48
  formatTime (current, isShow) {
    if (!current) {
      return
    }
    const date = new Date(current);
    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 mi = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
    const s = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
    if(isShow) {
      return `${y}${m}${d}${h}${mi}${s}`
    }
    return `${y}-${m}-${d} ${h}:${mi}:${s}`;
  }
}
export default new Time();
2、页面使用

index.vue

<script>
import timeUtil from '@/utils/time';

    methods: {
        getInfo(){
            let showTime = timeUtil.formatTime(new Date(new Date()), false)
            console.log('当前时间1', new Date());
            console.log('当前时间2', showTime);             
        }
    }
</script>

打印显示

当前时间1 Sat Feb 26 2022 09:38:58 GMT+0800 (中国标准时间)

当前时间2 2022-02-26 09:38:58

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值