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

后端 未结 3 1950
南旧
南旧 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.

提交回复
热议问题