0

I have an HTML5 date input element like this: <input type="date" />

if you choose a date in this input a string will be the value, for example: "2018-12-31"

In my model I want the date to be presented as the following string: "2018-12-31T00:00:00.000Z" and not the simplified string.

How do I make sure that the date in my model keeps the correct format? I tried using a computed but since I am in a loop this time I cannot use them.

{{eventDate.date}}
<b-form-input
    v-bind:value="eventDate.date"
    v-on:input="eventDate.date = $event.target.value"
    v-bind:id="'event-day-date-' + index"
    size="sm"
    type="date"
    placeholder="2018-12-31"
>
</b-form-input>

As you can see here the eventDate.date is a long string but the input needs the format YYYY-MM-DD. I need to convert it to and from this format some way. enter image description here

4
  • Can you show code that you have or already have tried? Commented Mar 18, 2018 at 11:19
  • @SuperDJ I added a code example with screenshot of the rendered HTML in the browser. Commented Mar 18, 2018 at 19:10
  • Altough this isn't an answer to your question but it's not required to use v-bind or v-on just :value or @input is enough Commented Mar 18, 2018 at 19:17
  • Yeah I read about that in the docs :). I used the v-on and v-bind to try to work out my problem. But didn't work. Is the problem now clear to you? If not let me know :) Commented Mar 18, 2018 at 19:22

2 Answers 2

1

You could use a filter:

filters: {
  dateToString(date) {
    return date.toString().substr(0,10)
  }
}

and then update your template

:value="eventDate.date | dateToString"
Sign up to request clarification or add additional context in comments.

1 Comment

It's filters in vue.js 2: v2.vuejs.org/v2/guide/filters
0

This is what I ended up using:

<input
    v-validate="'required'"
    v-bind:name="'eventdate-' + index"
    v-bind:value="eventDate.date | dateToString"
    v-on:input="eventDate.date = $event.target.value + 'T00:00:00.000Z'"
    v-bind:id="'event-day-date-' + index"
    class="form-control form-control-sm"
    type="date"
    placeholder="2018-12-31"
/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.