How to bind a proper date and time to an input in Vue3?

Advertisements

I’m trying to bind a current date and time to an input in Vue, which is a surprisingly hard task.

template:

<input type="datetime-local" v-model="dateRange.to">

script:

const dateRange = ref({
  from: null,
  to: inputDate
})

const today = new Date
const inputDate = today.toISOString().substring(0, 16)

With this code, I get time which is 2 hours earlier than my ‘now’.
How can I ‘make’ a date, that would: 1. have proper time. 2. be bindable to input type daytime-local (without an advanced string processing)?

>Solution :

You can subtract the timezone offset before calling toISOString.

const today = new Date;
const inputDate = new Date(today - today.getTimezoneOffset() * 60000).toISOString().slice(0, 16);

Leave a ReplyCancel reply