0

This code works well in most (all?) browsers:

    myDate = new Date();
    alert(myDate.toString().indexOf("("));

However, when executed within a VB.Net WebBrowser control, it returns -1.

Why is that?

iSofia
  • 1,412
  • 2
  • 19
  • 36
  • What's the point of the code?? – T.J. Crowder Aug 10 '15 at 06:13
  • 2
    _"It returns -1"_ because `"("` is not found in the result of `myDate.toString()` – Cerbrus Aug 10 '15 at 06:14
  • It's meant to strip the timezone out, which is displayed within brackets (). – iSofia Aug 10 '15 at 06:17
  • 1
    @iSofia: On the implementations you've seen before, perhaps, but that's not in any way required of it. The format is almost completely unspecified. – T.J. Crowder Aug 10 '15 at 06:17
  • In my real time tests, _myDate_ returns the following string: `Mon Aug 10 2015 14:21:55 GMT+0800 (Malay Peninsula Standard Time)` – iSofia Aug 10 '15 at 06:24
  • How could I strip out the `(Malay Peninsula Standard Time)`portion from the displayed date. – iSofia Aug 10 '15 at 06:28
  • @iSofia: If the string had `(Malay Peninsula Standard Time)` in it, `indexOf('(')` wouldn't return -1. So clearly it doesn't. – T.J. Crowder Aug 10 '15 at 06:38
  • 1
    Thank you all of you for your help. It turns out that VB.Net's web browser displays the date without the time zone, _and thus no brakcets, and accordingly indexOf() returns -1_. – iSofia Aug 10 '15 at 06:43

1 Answers1

5

Why is that?

Date's toString is not required to output a string that has any ( in it. If the string doesn't have a ( in it, indexOf will return -1.

From the specification:

  1. Let O be this Date object.
  2. If O does not have a [[DateValue]] internal slot, then
    • Let tv be NaN.
  3. Else,
    • Let tv be this time value.
  4. Return ToDateString(tv).

...where ToDateString says:

  1. Assert: Type(tv) is Number.
  2. If tv is NaN, return "Invalid Date".
  3. Return an implementation-dependent String value that represents tv as a date and time in the current time zone using a convenient, human-readable form.

So that could use no timezone indicator, or a Z to indicate GMT, or a timezone indicator that doesn't use (...) like GMT+04:00 or -05:00 or similar, etc.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Yes, it seems to be able to display the date string without using the _toString()_ function. But the date is displayed with the time zone: `Mon Aug 10 2015 14:21:55 GMT+0800 (Malay Peninsula Standard Time)`. Without using the _toString()_ function, I'm not able to use the _indexOf()_ function, to determine the location of the first bracket. – iSofia Aug 10 '15 at 06:26
  • @iSofia: I don't know what you mean by "display" in that sentence. `alert(new Date())` will use `toString`. If `toString` were returning a string with parentheses in it, `indexOf('(')` wouldn't return -1. So clearly it isn't. You're going to have to modify your code so it doesn't assume a `(`, since the specification is clear that none is required. – T.J. Crowder Aug 10 '15 at 06:37
  • Thank you, T.J. Crowder. In all haste I didn't bother with due diligence. For some reason, browsers tend to display the date with the timezone, but VB.Net's web browser control does not - _thus the -1 result_. So strange. – iSofia Aug 10 '15 at 06:41