Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set input type date's default value to today?

Tags:

html

date

input

Given an input element:

<input type="date" /> 

Is there any way to set the default value of the date field to today's date?

like image 967
Ian Brown Avatar asked Aug 08 '11 13:08

Ian Brown


People also ask

How can I get current date in field?

moment(). format('YYYY-MM-DD'); moment() returns an object representing the current date and time.

How can set current date in input type date in jquery?

Answers. //Get value of input text var dt = $("input[type=text]"). val(). split('-'); var date = dt[2] +"-"+ dt[1] +"-"+ dt[0];


1 Answers

Like any HTML input field, the browser will leave the date element empty unless a default value is specified within the value attribute. Unfortunately, HTML5 doesn't provide a way of specifying 'today' in the HTMLInputElement.prototype.value.

One must instead explicitly provide a RFC3339 formatted date (YYYY-MM-DD). For example:

element.value = "2011-09-29" 
like image 182
Tak Avatar answered Sep 23 '22 17:09

Tak