Html 输入类型 DateTime - 值格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10854874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input type DateTime - Value format?
提问by RobinJ
In which format should I put the date and time, for use in the HTML5 input element with datetime
type?
我应该以哪种格式输入日期和时间,以便在带datetime
类型的 HTML5 输入元素中使用?
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
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.
它们似乎都不起作用。
采纳答案by Hitham S. AlQadheeb
For <input type="datetime" value="" ...
为了 <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
表示全局日期和时间的字符串。
值:[RFC 3339] 中定义的有效日期时间,具有以下附加条件:
? 日期/时间语法中的文字字母 T 和 Z 必须始终大写
? date-fullyear 生产被定义为代表大于 0 的数字的四位或更多位数字
例子:
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
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.
此功能已过时。尽管它可能在某些浏览器中仍然有效,但不鼓励使用它,因为它可以随时被删除。尽量避免使用它。
HTML 是用于输入日期和时间(小时、分钟、秒和秒的分数)以及时区的控件。此功能已从 WHATWG HTML 中删除,并且不再受浏览器支持。
相反,浏览器正在实现(并鼓励开发人员使用)日期时间本地输入类型。
Why is HTML5 input type datetime removed from browsers already supporting it?
为什么 HTML5 输入类型 datetime 从已经支持它的浏览器中删除?
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime
回答by John Conde
This articleseems 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 onecovers using it in the <input>
field:
这个涵盖了在<input>
现场使用它:
<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).
<input type="date" name="d" min="2011-08-01" max="2011-08-15">
这个 HTML5 输入类型“日期”与属性 min 和 max 相结合的示例展示了我们如何限制用户可以输入的日期。属性 min 和 max 不相互依赖,可以独立使用。
<input type="time" name="t" value="12:00">
HTML5 输入类型“时间”允许用户选择以 24 小时格式显示的相应时间。如果我们不包括默认值“12:00”,时间将设置为用户本地机器的时间。
<input type="week" name="w">
HTML5 输入类型周将显示由“W”表示的周的数字版本以及相应的年份。
<input type="month" name="m">
HTML5 输入类型month 完全符合您的预期。它显示月份。准确地说,它显示月份的数字版本以及年份。
<input type="datetime" name="dt">
HTML5 输入类型 Datetime 显示 UTC 日期和时间代码。用户可以以一分钟为增量向前或向后更改时间步长。如果您希望显示用户的本地日期和时间,您将需要使用下一个示例 datetime-local
<input type="datetime-local" name="dtl" step="7200">
由于 datetime 每次步进一分钟,您可能希望使用属性“step”更改默认增量。在下面的示例中,我们将通过将属性 step 设置为 7200(60 秒 X 60 分钟 X 2)使其增加两个小时。
回答by Brandon Hill
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).
对于它的价值,与iOS7下探支持,datetime
你需要使用datetime-local
哪个不接受时区部分(这是有道理的)。
Doesn't work (iOS anyway):
不起作用(无论如何都是iOS):
<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):
PHP 价值(Windows 安全):
strftime('%Y-%m-%dT%H:%M:%S', strtotime($my_datetime_input))
回答by Jacob McKay
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:
该规范让我有点困惑,它说使用 RFC 3339,但是在我的 PHP 服务器上,当我使用 DATE_RFC3339 格式时,它没有初始化我的 hmtl 输入 :( PHP 的 DATE_RFC3339 常量是“Ymd\TH:i:sP “在撰写本文时,您应该删除时区信息(我们正在使用 datetime-LOCAL,伙计们)是有道理的。所以对我有用的格式是:
"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.
我认为能够在 datepicker 显示日期时设置 datepicker 的值更直观,但我猜它的显示方式因浏览器而异。
回答by Enigma Plus
This works for setting the value of the INPUT:
这适用于设置 INPUT 的值:
strftime('%Y-%m-%dT%H:%M:%S', time())
回答by Timo Braun
That one shows up correctly as HTML5-Tag for those looking for this:
对于那些寻找这个的人来说,那个正确地显示为 HTML5-Tag:
<input type="datetime" name="somedatafield" value="2011-12-21T11:33:23Z" />
回答by mh-cbon
with momentjs i use
我使用的momentjs
moment(value).format("YYYY-MM-DDTHH:mm:ss")