[removed] parse a string to Date as LOCAL time zone

后端 未结 3 1935
南旧
南旧 2020-11-27 18:14

I have a string representing the current time: 2015-11-24T19:40:00. How do I parse this string in Javascript to get a Date represented by this string as the

相关标签:
3条回答
  • 2020-11-27 18:49

    A variation on RobG's terrific answer.

    Note that this will require that you run bleeding edge JavaScript as it relies on the arrow notation and spread operator.

    function parseDateISOString(s) {
      let ds = s.split(/\D+/).map(s => parseInt(s));
      ds[1] = ds[1] - 1; // adjust month
      return new Date(...ds);
    }
    

    The benefit of this approach is that it automatically applies the correct number of arguments passed. If this is what you want.

    0 讨论(0)
  • 2020-11-27 18:50

    Parsing of date strings using the Date constructor or Date.parse (which are essentially the same thing) is strongly recommended against.

    If Date is called as a function and passed an ISO 8601 format date string without a timezone (such as 2015-11-24T19:40:00), you may get one of the following results:

    1. Pre ES5 implementaitons may treat it as anything, even NaN (such as IE 8)
    2. ES5 compliant implementations will treat it as UTC timezone
    3. ECMAScript 2015 compliant implementations will treat it as local (which is consistent with ISO 8601)

    A Date object has a time value which is UTC, and an offset based on system settings. When you send a Date to output, what you see is usually the result of Date.prototype.toString, which is an implementation dependent, human readable string representing the date and time, usually in a timezone based on system settings.

    The best way to parse date strings is to do it manually. If you are assured that the format is consistent and valid, then parsing an ISO format string as a local date is as simple as:

    /*  @param {string} s - an ISO 8001 format date and time string
    **                      with all components, e.g. 2015-11-24T19:40:00
    **  @returns {Date} - Date instance from parsing the string. May be NaN.
    */
    function parseISOLocal(s) {
      var b = s.split(/\D/);
      return new Date(b[0], b[1]-1, b[2], b[3], b[4], b[5]);
    }
    
    document.write(parseISOLocal('2015-11-24T19:40:00'));

    Note that parsing of ISO strings using Date.parse only accepts UTC, it does not accept any other timezone designation (noting the above behaviour if it's missing).

    0 讨论(0)
  • 2020-11-27 19:09

    Where Date is called as a constructor with more than one argument, the specified arguments represent local time.

    I also have a much faster way than using the string.split() because we already know where the numbers are:

    return new Date(Number(date.substring(0, 4)), Number(date.substring(5, 7))-1, 
                    Number(date.substring(8, 10)), Number(date.substring(11, 13)), 
                    Number(date.substring(14, 16)), Number(date.substring(17, 19)));
    

    This will work with and/or without the 'T' and 'Z' strings and still has decent performance. I added the explicit Number conversion (faster and better than parseInt) so this also compiles in TypeScript. Number

    0 讨论(0)
提交回复
热议问题