Html 如何从 HTML5 日期输入中解析日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16825363/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:54:49  来源:igfitidea点击:

How do you parse a date from an HTML5 date input?

javascripthtmlformsdateinput

提问by duckbrain

I have an input on my webpage that I am able to set the date on by getting an ISO string and pulling out the first 10 characters.

我的网页上有一个输入,我可以通过获取 ISO 字符串并提取前 10 个字符来设置日期。

date = new Date();
dateInput.value = date.toISOString().substr(0,10);

This works perfectly. My problem is that when I try to get the date back out. I am getting the date one day off.

这完美地工作。我的问题是,当我尝试恢复日期时。我要请一天假。

var newDate = new Date(dateInput.value);

I have also tried the following code to make up for it, but it is not always correct either

我也尝试了以下代码来弥补它,但它也不总是正确的

new Date(Date.parse(element.value) + 86400000)

新日期(Date.parse(element.value) + 86400000)

So my question is: Is there an elegant way to get the correct date consistently. I have been looking around for a little while, but it seems there is not a lot of consistency with date parsing in Javascript.

所以我的问题是:是否有一种优雅的方法可以始终如一地获得正确的日期。我已经环顾了一段时间,但似乎与 Javascript 中的日期解析没有太大的一致性。

采纳答案by Marty Kiker

It's interpreting the date as UTC, which, for most time zones, will make it seem like "yesterday". One solution could be to add the time-zone offset back into the date to "convert" it to your local timezone.

它将日期解释为 UTC,对于大多数时区,这将使它看起来像“昨天”。一种解决方案可能是将时区偏移量添加回日期以将其“转换”为您的本地时区。

回答by robertc

If it's an actual dateinput on a supporting browser, then it will have a valueAsDateproperty. There's no need to parse it.

如果它date是支持浏览器上的实际输入,那么它将具有valueAsDate属性。没有必要解析它。

回答by wezten

var newDate = new Date(dateInput.value + 'T00:00');

This will give the correct date in any timezone.

这将在任何时区给出正确的日期。

回答by JJ Spetseris

You can also convert the input string from YYYY-MM-DD to MM/DD/YYYY and it then parse the date to get the correct answer.

您还可以将输入字符串从 YYYY-MM-DD 转换为 MM/DD/YYYY,然后解析日期以获得正确答案。

EXAMPLE:

例子:

new Date(Date.parse('2018-09-28'))= Thu Sep 27 2018 19:00:00 GMT-0500 (Central Daylight Time)

new Date(Date.parse('2018-09-28'))= 2018 年 9 月 27 日星期四 19:00:00 GMT-0500(中部夏令时)

new Date(Date.parse('09/28/2018'))= Fri Sep 28 2018 00:00:00 GMT-0500 (Central Daylight Time)

new Date(Date.parse('09/28/2018'))= 2018 年 9 月 28 日星期五 00:00:00 GMT-0500(中部夏令时)

(NOTE: I am in CDT)

(注意:我在 CDT)

回答by Fabio Maulo

as said by Marty the problem is the difference between the representation of the timezone of the input (UTC defined by W3C) and the JS timezone (local). The solution is to getTimezoneOffset (which is in minutes) and convert everything to milliseconds:

正如马蒂所说,问题是输入时区(W3C 定义的 UTC)和 JS 时区(本地)的表示形式之间的差异。解决方案是 getTimezoneOffset (以分钟为单位)并将所有内容转换为毫秒:

var today = document.getElementById("myInputDate").valueAsDate;
var tomorrow = new Date(today.valueOf() + 86400000 + (today.getTimezoneOffset() * 60000));

86400000 = milliseconds of a day

86400000 = 一天的毫秒数

today.getTimezoneOffset() * 60000 = timezoneOffsetin milliseconds

today.getTimezoneOffset() * 60000 = timezoneOffset以毫秒为单位