当前位置:   article > 正文

关于时间框 date.getHours is not a function 报错问题解决

date.gethours is not a function

1.可以发现,页面上选择的只有日期,但是代码中却有 HH:mm:ss

value-format="yyyy-MM-dd HH:mm:ss"

2.而且代码中

type="date"

3.date.getHours is not a function应该与此有关

解决方案:

删除HH:mm:ss

经验证,可以正常的选择日期了。

.

拓展:

 如果想要 type="date" 页面上只展示日期 ,而向后端发送的时候是 yyyy-MM-dd HH:mm:ss 这种格式的话

  1. <el-date-picker
  2. :disabled="disabledFields.includes('battery_factory_date') && isRequired"
  3. class="common-datetime-picker no-close"
  4. popper-class="common-datetime-picker-select"
  5. type="date"
  6. placeholder="请选择"
  7. v-model="form.battery_factory_date"
  8. :picker-options="setDisabled"
  9. @change="getdatatime()"
  10. />

需要引入:

import { parseTime} from "@/utils/index";

utils/index文件

  1. /**
  2. * Parse the time to string
  3. * @param {(Object|string|number)} time
  4. * @param {string} cFormat
  5. * @returns {string | null}
  6. */
  7. export function parseTime(time, cFormat) {
  8. if (arguments.length === 0 || !time) {
  9. return null
  10. }
  11. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  12. let date
  13. if (typeof time === 'object') {
  14. date = time
  15. } else {
  16. if ((typeof time === 'string')) {
  17. if ((/^[0-9]+$/.test(time))) {
  18. // support "1548221490638"
  19. time = parseInt(time)
  20. } else {
  21. // support safari
  22. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  23. time = time.replace(new RegExp(/-/gm), '/')
  24. }
  25. }
  26. if ((typeof time === 'number') && (time.toString().length === 10)) {
  27. time = time * 1000
  28. }
  29. date = new Date(time)
  30. }
  31. const formatObj = {
  32. y: date.getFullYear(),
  33. m: date.getMonth() + 1,
  34. d: date.getDate(),
  35. h: date.getHours(),
  36. i: date.getMinutes(),
  37. s: date.getSeconds(),
  38. a: date.getDay()
  39. }
  40. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  41. const value = formatObj[key]
  42. // Note: getDay() returns 0 on Sunday
  43. if (key === 'a') {
  44. return ['日', '一', '二', '三', '四', '五', '六'][value]
  45. }
  46. return value.toString().padStart(2, '0')
  47. })
  48. return time_str
  49. }

methods 中 :

  1. getdatatime(value) {
  2. if (value == '') {
  3. this.form.battery_factory_date= "";
  4. } else {
  5. this.form.battery_factory_date =
  6. parseTime(this.form.battery_factory_date, "{y}-{m}-{d}") + " 00:00:00";
  7. }
  8. },

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号