AskYous
AskYous

Reputation: 4730

Input [type='datetime-local'] doesn't support seconds in Chrome

Using the datetime-local input type on Chrome doesn't seem to work when the value is set via Javascript and the value contains seconds. Why is this happening? It seems to be a general thing. In simple terms, here's how to re-create the problem:

  1. Create an input element with type='datetime-local'.
  2. Set the value to any date and time using JavaScript (using .value) with a non-zero seconds (eg. '1980-01-01T01:01:01.001') value.
  3. Try submitting the form. It returns a validation error.

But, if you set the seconds to 0 (eg. 1980-01-01T01:01:00.001), or the value is set via HTML attributes, then the validation error doesn't get thrown and the form submits fine.

See this CodePen sample: http://codepen.io/AskYous/pen/KNONVG

Upvotes: 2

Views: 2058

Answers (2)

int32_t
int32_t

Reputation: 6150

It's an expected behavior defined by the HTML standard.

The default step value for type=datetime-local is 1 minute. The default step-base is Unix epoch 1970-01-01T00:00:00. So 1980-01-01T01:01:01.001 causes a step-mismatch validation error.

If min attribute or value attribute is specified, it would be the step-base.

Upvotes: 1

AskYous
AskYous

Reputation: 4730

It seems it's a bug in Chrome. I found a solution. Use the .setAttribute() function:

datetimeInput.setAttribute('value', '1980-01-01T01:01:01.001');

Upvotes: 1

Related Questions