Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS set date value as Date instead of String

I need to change type of datepicker value and it should return value as type Date instead of String https://prnt.sc/r6vr3g. But i don't know how can i make it. Can anyone pls help me?

Here is code:

<template>
  <q-input
@focusin="onFocusIn"
:value="value"
@input="e => $emit('input', e.toString())"
@click="alert = true"
 >
<q-dialog v-model="alert">
  <q-date
    :value="value" @input="onInput"
    :mask="mask"
  />
</q-dialog>

<script>

    import _ from 'lodash'

    export default {
  props: {
    ..props
  },

  data () {
    return {
      alert: false,
      sValue: ''
    }
  },

  ..computed

  methods: {
    onInput (e) {
      let dateObj = new Date(e)
      this.$emit('input', dateObj)
      this.alert = false
    },

    onFocusIn (e) {
      e.target.blur()
    }
  }

}

</script>

<style type="text/css">

</style>


<div class="col">
   <s-datetime-picker v-model="data.dateStart" label="Date Start" required />
   {{ data.dateStart }}
</div>

Here is code for datepicker component and after that there si example of using this component.

I've edited code, because i changed component. Now i have another error, in input field it shows message 'Invalid Date' and in console i got this error "failed for prop "value". Expected String with value "Invalid Date", got Date"

like image 422
kavkax Avatar asked Sep 06 '25 02:09

kavkax


1 Answers

Format your emitting object before emit

<template>
  <q-datetime-picker
    ..more properties
    :value="value"
    @input="formatDate(e)"
  />
</template>

<script>

import _ from 'lodash'

export default {
  props: {
    ...all properties
  },

  computed: {

    sLabel () {
      if (!this.required || _.isUndefined(this.label)) return this.label
      return this.label + ' *'
    },

    sRules () {
      if (!this.required) return this.rules

      let rule = val => { if (val.length === 0) return 'This field is Required' }
      if (_.isUndefined(this.rules)) return [ rule ]

      return (_.cloneDeep(this.rules)).push(rule)
    }

  },
 formatDate(val){
   let dateObj = new Date(val);
   this.$emit('input', dateObj);
 }

}

</script>

<style type="text/css">

</style>


<div class="col">
   <s-datetime-picker v-model="data.dateStart" label="Date Start" required />
   {{ data.dateStart }}
</div>

Format your date according to your need in formatDate function. Hope this helps you,

like image 200
Purushoth.Kesav Avatar answered Sep 08 '25 14:09

Purushoth.Kesav