-1

console.log(
  ('00' + (new Date('3/4/2019')).toLocaleDateString("en-US").split('/')[1]).slice(-2)
);

IE:

"4"

Chrome

"04"

What is the workaround?

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Garfield
  • 1,192
  • 3
  • 12
  • 22
  • Why do you consider `04` correct? I don't write Star Wars day as May 04th, but May 4th. – Jon P Mar 19 '19 at 03:09
  • @Garfield, Is your issue still persist or you got a solution from that other thread suggested by the community members? Please let us know about the current status of the issue. We will try to provide further suggestions if needed. Thanks for your understanding. – Deepak-MSFT Mar 26 '19 at 09:42
  • The duplicate thread explains IE11 adds hidden characters in toLocaleDateString() so .slice() does not work. My solution is similar to the accepted answer, but there are also acceptable workarounds offered on the duplicate thread. – Garfield Mar 26 '19 at 21:40

1 Answers1

3

Why not just to get the day from the available method from the object Date.

var day = new Date('3/4/2019').getDate();
console.log(day >= 10 ? day : ("0" + day)); // We can use String.prototype.padStart(2, "0"), however, that's not available in IE11
Ele
  • 33,468
  • 7
  • 37
  • 75