如何将这种"2023-05-25T16:00:00.000Z"时间格式转换为“2023-03-08 16:46:15”使用vue3+ts的element-plus组件

如何将这种"2023-05-25T16:00:00.000Z"时间格式转换为“2023-03-08 16:46:15”使用vue3+ts的element-plus组件

这个用 moment.js 或者day.js 就行吧。 一般 安装了 element 都会自带 moment 然后moment.formater yyyy-MM-dd HH:mm:ss .

或者用 js 里的date对象

<template>
  <div>
    <el-date-picker
      v-model="date"
      type="datetime"
      value-format="yyyy-MM-ddTHH:mm:ss.SSSZ"
      format="yyyy-MM-dd HH:mm:ss"
      @change="onDateChange"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { ElDatePicker } from 'element-plus';

export default defineComponent({
  components: {
    ElDatePicker,
  },
  setup() {
    const date = ref('');

    const onDateChange = (value: Date) => {
      if (value) {
        const year = value.getUTCFullYear();
        const month = value.getUTCMonth() + 1;
        const day = value.getUTCDate();
        const hours = value.getUTCHours();
        const minutes = value.getUTCMinutes();
        const seconds = value.getUTCSeconds();

        const formatted = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        
        // 将格式化后的时间设置到响应式变量中
        date.value = formatted;
      }
    };

    return {
      date,
      onDateChange,
    };
  },
});
</script>

在模板中,使用el-date-picker组件来选择日期和时间。该组件的value-format属性用于设置组件返回的Date对象的格式,format属性用于设置显示在组件中的时间格式。

在setup()函数中,使用ref()函数创建了一个响应式变量date来保存转换后的时间。在onDateChange()函数中,使用Date对象中的方法获取每个时间部分的值,并使用字符串拼接将其格式化为"yyyy-MM-dd HH:mm:ss"格式的字符串。最后,将格式化后的时间设置到响应式变量中。

这样,就可以将"2023-05-25T16:00:00.000Z"时间格式转换为“2023-03-08 16:46:15”的格式了。

直接写个函数转换就行啦

function format(str){
    let date = new Date(str);
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}

img

  • 这篇博客: vue3+element-plus上传文件,预览文件中的 vue3+ts+element-plus上传文件,预览文件 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 以下回答由chatgpt基于相关博客总结生成:

    问题解答:

    将时间格式从"2023-05-25T16:00:00.000Z"转换为 "2023-03-08 16:46:15",可按照如下步骤进行:

    1.使用Vue3和Element Plus组件

    在Vue3项目中安装Element Plus组件库,在需要使用的组件中引入需要的组件,例如el-date-picker日期选择器等。

    2.获取时间数据

    假设时间数据为time,可以通过以下语句获取时间对象:

    const date = new Date(time);
    

    3.格式化时间

    通过以下语句格式化时间,并输出为需要的字符串格式。

    const year = date.getFullYear();
    const month = (date.getMonth()+1).toString().padStart(2,'0');
    const day = (date.getDate()).toString().padStart(2,'0');
    const hour = (date.getHours()).toString().padStart(2,'0');
    const minute = (date.getMinutes()).toString().padStart(2,'0');
    const second = (date.getSeconds()).toString().padStart(2,'0');
    const newTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    console.log(newTime); 
    

    最后,newTime就是我们需要的字符串格式"2023-03-08 16:46:15"。

    完整代码示例:

    <template>
      <div>
        时间格式转换示例:
        {{ time }} 转为 {{ newTime }}
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          time: "2023-05-25T16:00:00.000Z",
          newTime: ""
        };
      },
      mounted() {
        const date = new Date(this.time);
        const year = date.getFullYear();
        const month = (date.getMonth()+1).toString().padStart(2,'0');
        const day = (date.getDate()).toString().padStart(2,'0');
        const hour = (date.getHours()).toString().padStart(2,'0');
        const minute = (date.getMinutes()).toString().padStart(2,'0');
        const second = (date.getSeconds()).toString().padStart(2,'0');
        this.newTime = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
      }
    };
    </script>