Skip to content

Instantly share code, notes, and snippets.

@spcheema
Last active February 28, 2019 23:01
Show Gist options
  • Save spcheema/d8b6d772327585b91e0797e0684b3072 to your computer and use it in GitHub Desktop.
Save spcheema/d8b6d772327585b91e0797e0684b3072 to your computer and use it in GitHub Desktop.
Current date object conversion based on the given timezone string.
/**
* Return current date object according to given timezone. Date will be return based on system's timezone an error occured while parsing
*
* @param timezone Timezone string e.g. `America/Los_Angeles`
* @returns {Date}
*/
function currentDate(timezone) {
try {
return new Date(new Date().toLocaleString("en-US", {timeZone: timezone}));
} catch (error) {
console.error(error);
console.info('Date object is being returned based on the system\'s timezone');
}
return new Date();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment