Last Updated: February 25, 2016
·
1.15K
· iantearle

Setting input[type=date] with Javascript

To save a lot of head scratching, if you need to programatically set a date input with Javascript you will need to pass the string as toISOString() and remove additional characters from the end using substring().

You may run into timezone issues, setting the Date.UTC object does the trick.

new Date(Date.UTC(this.currentYearView, this.currentMonthView, e.target.innerHTML)).toISOString().substring(0, 10);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside