Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading