Input type DateTime - Value format?

HtmlDatetimeHtml Input

Html Problem Overview


In which format should I put the date and time, for use in the HTML5 input element with datetime type?

I have tried:

  • 1338575502
  • 01/06/2012 19:31
  • 01/06/2012 19:21:00
  • 2012-06-01
  • 2012-06-01 19:31
  • 2012-06-01 19:31:00

None of them seem to work.

Html Solutions


Solution 1 - Html

For <input type="datetime" value="" ...

> A string representing a global date and time. > > Value: A valid date-time > as defined in [RFC 3339], with these additional qualifications:
> > •the literal letters T and Z in the date/time syntax must always be uppercase > > •the date-fullyear production is instead defined as four or > more digits representing a number greater than 0 > > Examples: > > 1990-12-31T23:59:60Z > > 1996-12-19T16:39:57-08:00

http://www.w3.org/TR/html-markup/input.datetime.html#input.datetime.attrs.value

Update:

> This feature is obsolete. Although it may still work in some browsers, > its use is discouraged since it could be removed at any time. Try to > avoid using it. > > The HTML was a control for entering a date and > time (hour, minute, second, and fraction of a second) as well as a > timezone. This feature has been removed from WHATWG HTML, and is no > longer supported in browsers. > > Instead, browsers are implementing (and developers are encouraged to > use) the datetime-local input type.

https://stackoverflow.com/questions/21263515/why-is-html5-input-type-datetime-removed-from-browsers-already-supporting-it/21291028

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime

Solution 2 - Html

For what it's worth, with iOS7 dropping support for datetime you need to use datetime-local which doesn't accept timezone portion (which makes sense).

Doesn't work (iOS anyway):

 <input type="datetime-local" value="2000-01-01T00:00:00+05:00" />

Works:

<input type="datetime-local" value="2000-01-01T00:00:00" />

PHP for value (windows safe):

strftime('%Y-%m-%dT%H:%M:%S', strtotime($my_datetime_input))

Solution 3 - Html

This article seems to show the valid types that are acceptable

<time>2009-11-13</time>
 <!-- without @datetime content must be a valid date, time, or precise datetime -->
<time datetime="2009-11-13">13<sup>th</sup> November</time>
 <!-- when using @datetime the content can be anything relevant -->
<time datetime="20:00">starting at 8pm</time>
 <!-- time example -->
<time datetime="2009-11-13T20:00+00:00">8pm on my birthday</time>
 <!-- datetime with time-zone example -->
<time datetime="2009-11-13T20:00Z">8pm on my birthday</time>
 <!-- datetime with time-zone “Z” -->

This one covers using it in the <input> field:

> <input type="date" name="d" min="2011-08-01" max="2011-08-15"> This > example of the HTML5 input type "date" combine with the attributes min > and max shows how we can restrict the dates a user can input. The > attributes min and max are not dependent on each other and can be used > independently. > > <input type="time" name="t" value="12:00"> The HTML5 input type > "time" allows users to choose a corresponding time that is displayed > in a 24hour format. If we did not include the default value of "12:00" > the time would set itself to the time of the users local machine. > > <input type="week" name="w"> The HTML5 Input type week will display > the numerical version of the week denoted by a "W" along with the > corresponding year. > > <input type="month" name="m"> The HTML5 input type month does > exactly what you might expect it to do. It displays the month. To be > precise it displays the numerical version of the month along with the > year. > > <input type="datetime" name="dt"> The HTML5 input type Datetime > displays the UTC date and time code. User can change the the time > steps forward or backward in one minute increments. If you wish to > display the local date and time of the user you will need to use the > next example datetime-local > > <input type="datetime-local" name="dtl" step="7200"> Because > datetime steps through one minute at a time, you may want to change > the default increment by using the attribute "step". In the following > example we will have it increment by two hours by setting the > attribute step to 7200 (60seconds X 60 minutes X 2).

Solution 4 - Html

This was a good waste of an hour of my time. For you eager beavers, the following format worked for me:

<input type="datetime-local" name="to" id="to" value="2014-12-08T15:43:00">

The spec was a little confusing to me, it said to use RFC 3339, but on my PHP server when I used the format DATE_RFC3339 it wasn't initializing my hmtl input :( PHP's constant for DATE_RFC3339 is "Y-m-d\TH:i:sP" at the time of writing, it makes sense that you should get rid of the timezone info (we're using datetime-LOCAL, folks). So the format that worked for me was:

"Y-m-d\TH:i:s"

I would've thought it more intuitive to be able to set the value of the datepicker as the datepicker displays the date, but I'm guessing the way it is displayed differs across browsers.

Solution 5 - Html

This works for setting the value of the INPUT:

strftime('%Y-%m-%dT%H:%M:%S', time())

Solution 6 - Html

That one shows up correctly as HTML5-Tag for those looking for this:

<input type="datetime" name="somedatafield" value="2011-12-21T11:33:23Z" />

Solution 7 - Html

with momentjs i use

moment(value).format("YYYY-MM-DDTHH:mm:ss")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobinJView Question on Stackoverflow
Solution 1 - HtmlSullyView Answer on Stackoverflow
Solution 2 - HtmlBrandon HillView Answer on Stackoverflow
Solution 3 - HtmlJohn CondeView Answer on Stackoverflow
Solution 4 - HtmlJacob McKayView Answer on Stackoverflow
Solution 5 - HtmlEnigma PlusView Answer on Stackoverflow
Solution 6 - HtmlTimo BraunView Answer on Stackoverflow
Solution 7 - Htmlmh-cbonView Answer on Stackoverflow