1

I have a date function that converts HTML form input into a date in milliseconds. It works in all browsers except Internet explorer.

Is the JavaScript Date function not functional in IE?

The code below should give you 1521032400 in FF and Chrome but NaN in IE11

Code

var startTime = '9:00 AM';
var startDate = "2018-03-14";

var dateInMilli = new Date(startDate + " " + startTime.split(' ')[0]).getTime() / 1000;

alert(dateInMilli);
PT_C
  • 1,178
  • 5
  • 24
  • 57

2 Answers2

2

The Date function does work in IE, it's just more finicky about what strings it accepts. If you format your string to the RFC2822 standard ("Wed, 14 Mar 2018 09:30:00 GMT") or the ISO standard ("2018-03-14T09:00:00") you should be ok in any browser.

Steve Archer
  • 641
  • 4
  • 10
  • Emphasise the word "should". It doesn't "work" correctly in Safari at least, and RFC2822 formatting is only supported in the latest version of ECMA-262: ECMAScript 2018, so ubiquitous support can't be assumed. – RobG Jul 10 '18 at 03:46
1

The format you use isn't standard, so IE11 doesn't have to understand it.

Use the following format instead:

new Date("2018-03-14T09:00")
Nicklaus Brain
  • 884
  • 6
  • 15
  • 1
    No, don't do that either. It should be treated as local, but Safari treats it as UTC. Please follow the advice in the duplicate: **do not use the built-in parser**. Manually parse date strings with a custom function or library. – RobG Jul 10 '18 at 03:44
  • @RobG: Is there any reason for not using `Date.parse` with full ISO 8601 (i.e. with TZ specifier)? – Amadan Jul 13 '18 at 10:07
  • @Amadan—it's probably OK now in most browsers, but in the Internet of Things there are vastly more implementations than just the popular handful of browsers that most can name, so I would avoid it. A parser for a single format is only a few lines of code, so why not? – RobG Jul 13 '18 at 12:36