- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 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 种日期表示:
// 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/
数据框有一个字符串类型的日期列 '2017-01-01' 它被转换为 DateType() df = df.withColumn('date', col('date_string').cast(Dat
这个问题在这里已经有了答案: What is "x && foo()"? (5 个答案) 关闭 8 年前。 我在 bootstrap-datepicker.js 文件中遇到过这个。 作者在_setD
我有一个数据库 utc 字符串,我正在传递到 Date(attrs.endDate),然后通过 new Date() 减去当前的 utc 日期,但我无法得到它来为我提供 2 个 utc 日期的正确差异
这个问题在这里已经有了答案: how to determine if 2 dates object equals each other? [duplicate] (3 个答案) 关闭 6 年前。 我
这个问题已经有答案了: How can I convert "/Date(1399739515000)/" into date format in JavaScript? (3 个回答) 已关闭 8
根据MDN ,我们只能将以下类型的参数传递给 Date 构造函数: new Date(); new Date(value); // Unix timestamp new Date(dateString
我从表中获取所有项目: endDate >= 现在 endDate 为 NULL published 等于 1。 这是我所拥有的,但它给了我 0 个项目: $items = Items::orderB
此查询需要很长时间才能完成。当我将 WHERE 子句设置为 new_dl >= '2014-01-01' 时,查询大约需要 6 分钟才能浏览大约 3 个月的数据。现在不知道为什么这个应该从 12 个月
我有一个正在为项目开发的小型 Java 程序,它使用 JavaMail 从指定的 URI 中提取用户的收件箱,然后开始处理消息。 在 Outlook 中,属性菜单中有一个功能可以设置邮件的到期日期,它
我想在获取 Date.getHours()、Date.getMinutes() 和 Date.getSeconds() 的值后格式化输出>. 这是一条漫长的路: var dt = new Date()
我发现java.text.DateFormat有两种格式化日期的方法。一种是采用 Date 参数,另一种是采用 Object 参数。我检查了DateFormat源代码,似乎他们调用了不同的内部方法。
我有两个对象,p4 和 p5,它们都具有 Date 属性。在某些时候,构造函数工作正常: p4.setClickDate(new Date(System.currentTimeMillis() - 8
我是使用 Sequelize 和 Node.js 的新手,但我的代码中存在日期比较问题。 User.findOne({ where: { resetToken: passwordToken,
我正在使用一个名为 fullcalendar 的 jquery 日历。当用户单击某一天时,他们将被发送到另一个页面以创建该天的事件。单击的日期作为 date 提供。然后通过下面的函数运行将其转换为 U
我有一个列表列表,每个列表中都有整数值,代表 8 年期间的日期。 dates = [[2014, 11, 14], [2014, 11, 13], ....., [2013, 12, 01]
我有两个表: 首先是TimeValues(示例) time | value 12/28/18 | 5.6 01/03/19 | 5.6 01/04/19 | 5.6 01/09/19 | 5.
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
像这样实例化的日期对象: new Date("2011-12-13") 返回一个认为自己是星期一的日期对象: Date {Mon Dec 12 2011 16:00:00 GMT-0800 (PST)
我需要选择入住日期和退房日期在指定日期范围之间的房价。这些费率根据其条件单独命名。房费取决于所选日期。这是我的代码: rate_eb rate_name rate_starts rat
我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天
我是一名优秀的程序员,十分优秀!