且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

移动Safari(10.3.1)DateTime-Local“输入有效值"错误

更新时间:2023-10-10 18:16:52

简单的解决方案!

IOS要求在输入字段上设置类型为"datetime-local"的值.

IOS requires a value to be set on an input field with a type of "datetime-local".

示例:<input type="datetime-local" value="2000-07-01T12:00" />

就这样:)

我个人觉得将默认值设置为用户当前的本地时间很好.这必须在ISOTime中格式化,且没有秒,因此其代码可能类似于:

I personally find it nice to set the default value to the users current local time. This has to be formatted in ISOTime without seconds, so the code for this might be something like:

// get the iso time string formatted for usage in an input['type="datetime-local"']
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0,-1);
var localISOTimeWithoutSeconds = localISOTime.slice(0,16);

// select the "datetime-local" input to set the default value on
var dtlInput = document.querySelector('input[type="datetime-local"]');

// set it and forget it ;)
dtlInput.value = localISOTime.slice(0,16);