Skip to content

Instantly share code, notes, and snippets.

@ImanMh
Created April 23, 2018 10:49
Show Gist options
  • Save ImanMh/bc34c79a932c307b5d6b63f6406fa597 to your computer and use it in GitHub Desktop.
Save ImanMh/bc34c79a932c307b5d6b63f6406fa597 to your computer and use it in GitHub Desktop.
Passing a day and a timezone offset and getting back local time.
const getLocalTime = (date, offset) => {
const utc = date.getTime() - (date.getTimezoneOffset() * 60000);
const corrected = new Date(utc + (3600000 * offset));
return {
hours: corrected.getHours(),
minutes: corrected.getMinutes(),
};
};
// Tehran Local Time
getLocalTime(new Date(), '-4.5');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment