Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
input-date for vue3
// adapted from https://acdcjunior.github.io/how-bind-date-object-to-input-date-vue.js-v-model.html
{
props: ['modelValue'],
emits: ['update:modelValue'],
setup() {
const dateToYYYYMMDD = (d) => {
// alternative implementations in https://stackoverflow.com/q/23593052/1850609
try {
return d && new Date(d.getTime()-(d.getTimezoneOffset()*60*1000)).toISOString().split('T')[0];
} catch(e) {
return null;
}
};
return {
dateToYYYYMMDD,
}
},
template: `
<input
v-model="value"
type="date"
@input="$emit('update:modelValue', $event.target.valueAsDate)"
/>
`,
computed: {
value: {
get() {
return this.dateToYYYYMMDD(this.modelValue);
},
set(value) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment