Last active
February 28, 2019 23:01
-
-
Save spcheema/d8b6d772327585b91e0797e0684b3072 to your computer and use it in GitHub Desktop.
Current date object conversion based on the given timezone string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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