1

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 format date is parsed that does not contain timezone information, UTC is assumed (Under section "Differences in assumed time zone").

I have a local datetime string with no timezone information: 2015-09-08T11:27:30

How can I get Date.parse to parse this as a local time?

ConditionRacer
  • 4,418
  • 6
  • 45
  • 67

1 Answers1

2

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

function localizeDateStr(date_to_convert_str) {
  var date_to_convert = new Date(date_to_convert_str);
  var local_date = new Date();
  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.

Community
  • 1
  • 1
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102