【笔记】将时间转换为xx年xx月xx日xx时xx分xx秒格式

原因:中国人习惯上都是以xx年xx月xx日xx时xx分xx秒开始的

需求:将以下时间转为xx年xx月xx日xx时xx分xx秒格式

  1. 毫秒数(例如:1642067638245)
  2. Fri Jan 01 2021 00:00:00 GMT+0800 (中国标准时间)
  3. 2020/01/01
  4. … …

转化:

<template>
   <div>
      时间:{{date}}
   <div>
<template>
<script>
   export default {
    data() {
      return {
        date:'',
        result:{},
        httpData:{
          id:''
        }
      }
    },
    methods:{
      changeTime(){
        // vue3 获取接口
        this.$api['接口地址'](this.httpData).then((res) => {
           // 打印res,假如是从res.data.date中取出时间,那么
           this.result = res.data
           // 一定要先创建时间对象
           let DateObj = new Date(this.result.date)
           // 将时间转换为 XX年XX月XX日XX时XX分XX秒格式
           let year = DateObj .getFullYear()
           let month = DateObj .getMonth() + 1
           let day = DateObj .getDate()
           let hh = DateObj .getHours()
           let mm = DateObj .getMinutes()
           let ss = DateObj .getSeconds()
           month = month > 9 ? month : '0' + month
           day = day > 9 ? day : '0' + day
           // 最终时间
           this.date = `${year}年${month}月${day}日${hh}时${mm}分${ss}秒
      }
    }
   }
</script>

效果:
【笔记】将时间转换为xx年xx月xx日xx时xx分xx秒格式

上一篇:获取当前月第一天,当前月最后一天,上个月日期,上个月的第一天


下一篇:PHP 中使用 strtotime "+1 month" 时发现的坑