gpt4 book ai didi

javascript - 如何检测浏览器的输入格式[type=date]

转载 作者:太空狗 更新时间:2023-10-29 15:41:24 29 4
gpt4 key购买 nike

使用 Javascript,如何检测日期输入字段 (input[type=date]) 中的日期格式?

类似 Modernizr 的工具仅检测使用 HTML5 日期选择器的能力(尤其适用于移动设备及其用于选择日期的 UI)。但是,我还没有找到任何解决方案来检测浏览器使用的格式。

我有一个这样的输入框:

<input type="date" class="border-radius" min="2013-12-21" max="2015-12-20" data-persist-local="FromDate">

如果在 localStorage 中找到该值,则使用 jQuery 调用 .val() 输入该值。我认为由于 min 和 max 属性的有效日期格式是有效的 ISO 格式,所以获取字段的值将返回相同的格式。事实证明,事实并非如此。

在以瑞典语为主要语言的 Chrome 浏览器 (31.0.1650.63 m) 上进行测试会给出 ISO 格式,但使用以丹麦语为主要语言的同一浏览器会将日期格式反转为 dd-mm-yyyy 而不是 yyyy-mm-日。我怀疑其他语言环境也是如此。

我不想change the input format - 理想情况下会有一个函数通过一致的 "wire format" 获取日期但除了显示格式,我还没有找到其他方法。

我如何检测输入字段的格式,并在 JS 中始终转换为有效日期,然后再转换回来?

最佳答案

ideally there would be a function to get the date by a consistent "wire format"

input.valueAsDate 方法返回反射(reflect)输入当前值的 Date 对象。

根据实验(编辑:2013 年 12 月 20 日的 Chrome 和 Opera 桌面版),日期输入的显示值似乎遵循相同的格式与 new Date().toLocaleDateString() 返回的一样,至少在 Chrome 中是这样。

前端有 3 种日期表示:

  1. Javascript 日期对象
  2. 输入属性和表面值的 ISO 日期字符串
  3. 输入向用户表示的日期字符串。

要从其他任何一个获取其中任何一个:

// From Javascript `Date` object to input value:
new Date().toISOString().substr( 0, 10 );

// From Javascript `Date` object to input display:
new Date().toLocaleDateString();

// From input value to Javascript `Date`:
input.valueAsDate;

// From input value to input display (step 3, then step 2):
input.valueAsDate.toLocaleDateString();

// From input display to Javascript `Date` object:
new Date( input.valueAsDate );

// From input display to input value (step 5, then step 1):
new Date( input.valueAsDate ).toISOString().substr( 0, 10 );

编辑:

以上恰好适用于 Chrome。 Desktop Safari 5.1.7 以 ISO 格式显示时间,这意味着您输入的就是用户看到的。 iOS Safari 显示一个下拉列表,其中包含格式为 dd mmm yyyy 的缩写月份名称。所以好像没有标准。

这里有一个小函数,可以为您提供仅适用于桌面版 Chrome 和 Opera 的正确转换:

var dateInput = ( function inputDateClosure(){
// Type checking
function getType( x ){
return Object.prototype.toString.call( x );
}

function isDate( x ){
return getType( x ) === '[object Date]';
}

function isInput( x ){
return getType( x ) === '[object HTMLInputElement]' || '[object Object]' && Object.prototype.hasOwnProperty.call( x, 'value' );
}

function isString( x ){
return getType( x ) === '[object String]';
}

function fromDateToValue( x ){
return x.toISOString().substr( 0, 10 );
}

function fromDateToString( x ){
return x.toLocaleDateString();
}

function fromInputToDate( x ){
return x.valueAsDate;
}

function fromStringToDate( x ){
return new Date( x.valueAsDate );
}

return function( x ){
if( isDate( x ) ){
return {
asDate : x,
asValue : fromDateToValue( x ),
asString : fromDateToString( x )
}
}
else if( isString( x ) ){
return {
asDate : fromStringToDate( x ),
asValue : fromDateToValue( fromStringToDate( x ) ),
asString : fromStringToDate( fromDateToString( x ) )
}
}
else if( isInput( x ) ){
return {
asDate : fromInputToDate( x ),
asValue : fromDateToValue( fromInputToDate( x ) ),
asString : fromDateToString( fromInputToDate( x ) )
}
}
}
}() );

关于javascript - 如何检测浏览器的输入格式[type=date],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703894/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com