簡體   English   中英

IE11中的日期字符串無法正常運行“ in”運算符

[英]“in” operator not working as expected with date strings in IE11

因此,我遇到了被轉換為字符串的Javascript Date對象與IE11中的in運算符之間的奇怪交互。 .toLocaleString()返回日期的字符串,在使用in運算符檢查鍵時不起作用,即使通過具有完全相同文本的另一個字符串也可以正常工作。

交互在Chrome中正常工作,但在IE11中似乎失敗。 我在這里缺少什么,有什么辦法可以解決?

 let days = { Monday: {}, Tuesday: {}, Wednesday: {}, Thursday: {}, Friday: {}, Saturday: {}, Sunday: {} }; // The example date should be a Monday let currentDate = new Date('2018/8/20'); let currentDay = currentDate.toLocaleString('en-us', { weekday: 'long' }); console.log(currentDay); console.log(typeof currentDay); console.log(currentDay in days); console.log('Monday' in days); 

感謝您的輸入!

這基本上就是為什么您應該堅持使用ISO日期字符串( 2018-01-01T00:00Z )而不使用任意格式的原因。

或者,自己解析格式,並使用new Date()構造函數的其他重載,如下所示:

const currentDate = new Date(2018, 7 /* months start at 0 */, 20 /* days do not */, 0, 0, 0)

日期解析很差,而IE則很糟糕,在這種情況下,兩個不好的地方不好。

這不是in這是錯誤的。 您的日期在IE中無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM