[Element-UI] 使用Element-UI的DateTimePicker组件报错:Cannot read property 'getHours' of undefined...

使用Element-UI组件的DateTimePicker,如下:

<template>
  <div class="block">
    <span class="demonstration">时间span>
    <el-date-picker
      v-model="value"
      type="datetime"
      placeholder="选择日期时间">
    el-date-picker>
  div>
template>

<script>
  export default {
    data() {
      return {
        value: '1554189000'
      };
    }
  };
script>

如果value 为时间戳或者yyyy-MM-dd hh:mm:ss格式,会报错:

TypeError: Cannot read property 'getHours' of undefined

因为date-picker 的时间是格林威时间,赋给v-model的值需要格林威时间格式,如果value 为yyyy-MM-dd hh:mm:ss,则只需要在 el-date-picker 节点上增加一个格式化value的属性 value-format="yyyy-MM-dd hh:mm:ss"

<el-date-picker
          v-model="value"
          type="datetime"
          value-format="yyyy-MM-dd hh:mm:ss"
          placeholder="选择日期时间">
 el-date-picker>

如果value 为时间戳,则只需要value = new Date(value * 1000) 转换一次即可

转载于:https://www.cnblogs.com/frost-yen/p/10643003.html

你可能感兴趣的:(ui,javascript)