Skip to content Skip to sidebar Skip to footer

Date.parse, Use Local Timezone When Timezone Is Not Specified In Iso Format Date

I tried searching for this and didn't find anything so please read the whole question before marking as a duplicate. The documentation for Date.parse specifies that when an ISO for

Solution 1:

Try this function, as mentioned in this comment on a similar question.

functionlocalizeDateStr(date_to_convert_str) {
  var date_to_convert = newDate(date_to_convert_str);
  var local_date = newDate();
  date_to_convert.setHours(date_to_convert.getHours() + (local_date.getTimezoneOffset()/60));
  return date_to_convert.toString();
}

alert(localizeDateStr('2015-09-08T11:27:30'));

This will get the timezone offset from a local date (divide by 60 to convert minutes to hours), then add that number of hours to the date parsed from the string you want to localize. Then, converting it to a string will show you the correct local time with time zone.

Or, you can just return date_to_convert; to get the date object rather than a string.

Post a Comment for "Date.parse, Use Local Timezone When Timezone Is Not Specified In Iso Format Date"