G
G
Greeg Zagrelov2022-02-07 14:21:14
JavaScript
Greeg Zagrelov, 2022-02-07 14:21:14

How to display the year, month and day separately?

When studying vue, I encountered such a problem as outputting all days, months and years to separate selects.
just like in the example. So that when choosing a month, the days would stretch themselves
6200ffe786c51039652609.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2022-02-07
@OldSchool1705

data: () => ({
  year: null,
  month: null,
  day: null,
}),
computed: {
  daysCount() {
    const { year, month } = this;
    return year !== null && month !== null
      ? new Date(this.year, this.month + 1, 0).getDate()
      : 0;
  },
  date() {
    const { year, month, day } = this;
    return year !== null && month !== null && day !== null
      ? new Date(year, month, day)
      : null;
  },
},
watch: {
  daysCount(val) {
    if (this.day) {
      this.day = Math.min(this.day, val);
    }
  },
},

<select v-model="year">
  <option v-for="i in 30">{{ 2000 + i }}</option>
</select>
<select v-model="month">
  <option v-for="i in 12" :value="i - 1">
    {{ new Date(0, i - 1).toLocaleDateString('ru-RU', { month: 'long' }) }}
  </option>
</select>
<select v-model="day">
  <option v-for="i in daysCount">{{ i }}</option>
</select>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question