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

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



So I came across this odd interaction between Javascript Date objects being converted into Strings and the in operator in IE11. The String that .toLocaleString() returns for a date, doesn't work when checking for keys using the in operator, even through another string with the exact same text works just fine.


in


.toLocaleString()


in



The interaction works correctly in Chrome, but seems to fail in IE11. What am I missing here and is there a way to get around this?




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);



Thanks for any input!





You've apparently missed "Invalid date" log in IE ..?
– Teemu
Aug 20 at 18:10





What do you mean?
– Chase Ingebritson
Aug 20 at 18:14





console.log(currentDay); logs "Invalid date" in IE11.
– Teemu
Aug 20 at 18:14


console.log(currentDay);





Odd, sorry about that. I was getting a valid date on my localhost. I’ll double check when I get back to my computer.
– Chase Ingebritson
Aug 20 at 18:28





@teemu—but then you'll get an invalid Date in Safari. ;-)
– RobG
Aug 20 at 20:31





1 Answer
1



This is basically why you should stick to ISO date strings (2018-01-01T00:00Z) and not use arbitrary formats.


2018-01-01T00:00Z



Alternatively, parse the format yourself and use the other overload of the new Date() constructor like so:


new Date()


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



Date parsing is bad, and IE is bad, and two bads don't make a good in this case.



It's not the in that's wrong. Your date is invalid in IE.


in





I guess I'm misunderstanding how if I output Monday from a Date object as a String and I just initialize my own String with the text Monday how they aren't equivalent.
– Chase Ingebritson
Aug 20 at 21:16


Monday


Monday





How is IE worse than Safari?
– Teemu
Aug 21 at 4:10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj