简体   繁体   English

HTML输入类型的日期和时间,不带时区偏移

[英]HTML input type date and time without timezone offset

I am using HTML5 input date and input time for ionic development. 我正在使用HTML5输入日期和输入时间进行离子显影。 By default, it binds as ISO Date String and change to UTC date time. 默认情况下,它绑定为ISO日期字符串,并更改为UTC日期时间。 It seems wrong to me as when user choose the date 2016-06-06, it may have become 2016-06-07 or 2016-06-05 depending on timezone offset. 当用户选择日期2016-06-06时,这对我来说似乎是错误的,取决于时区偏移量,它可能已变为2016-06-07或2016-06-05。 Similar situation for input type = time. 输入类型=时间的情况类似。

What my intention is to make the Date in ISO String to remain of what user chosen instead of offset it with timezone difference. 我的目的是使ISO字符串中的日期保留用户选择的日期,而不是用时区差补偿它。

Something like input datetime-local but seems that datetime-local is not supported by mobile device. 类似于输入datetime-local,但似乎移动设备不支持datetime-local。

I am finding something like input type="date-local" or "time-local". 我发现类似输入type =“ date-local”或“ time-local”的东西。

First of all it is interesting that type="datetime" has been removed from the HTML5 standard and instead only "datetime-local" exists, yet it appears that not every mobile browser implements it. 首先,有趣的是, 已从HTML5标准中删除了type =“ datetime”,而只存在“ datetime-local”,但似乎并不是每个移动浏览器都实现它。 For type="date", it doesn't have a time component, so just use the UTC date directly. 对于type =“ date”,它没有时间成分,因此直接使用UTC日期即可。 True, converting a UTC date d to local is kind of ridiculous: 的确,将UTC日期d转换为本地有点荒谬:

  • new Date(d.toLocaleDateString()) 新日期(d.toLocaleDateString())

or 要么

  • d.setMinutes(d.getMinutes()+d.getTimezoneOffset())

or 要么

  • new Date(+d+d.getTimezoneOffset()*60000)

but what can you do? 但你能做什么呢?

Javascript dates are always UTC, but you can get the local values as well as shown here: http://www.w3schools.com/jsref/jsref_obj_date.asp Javascript日期始终为UTC,但是您可以获取本地值以及此处显示的内容: http : //www.w3schools.com/jsref/jsref_obj_date.asp

getUTCHours() Returns the hour, according to universal time (from 0-23) getHours() Returns the hour (from 0-23) getUTCHours()返回小时,根据通用时间(从0-23开始)getHours()返回小时(从0-23开始)

The first one will always be translated to UTC from what was inputed. 第一个始终将根据输入内容转换为UTC。 The second one will use the timezone offset to calculate what should be returned. 第二个将使用时区偏移量来计算应返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM