Convert date to another timezone and format to local standards

/** * Convert the devices datetime to a specific timezone and display it in a specific format. So if you are in Germany and the clock in the kitchen tells you it's „13:15”, but your account is American and bound to NYC, the output of this function would be something like „7:15 am”. * @param {String} locales ISO 3166 Alpha-2 code like 'DE' or 'EN' (@see {@link}) * @param {String} timeZone Area/Location like "Europe/Berlin" or "America/New_York" (@see {@link}) * @see {@link} * @returns {String} Time like '12:10:23' or '12:10:23 AM' (based on param locales) */ function foo(locales, timeZone) { return new Date().toLocaleTimeString(locales, { timeZone: timeZone }); } /* Example */ foo("EN", "America/New_York"); /* -> 07:40:37 AM (English format, current time in NYC) */ foo("DE", "America/New_York"); /* -> 07:40:37 (German format, current time in NYC) */ foo("EN", "Europe/Berlin"); /* -> 1:40:37 PM (English format, current time in Berlin) */ foo("DE", "Europe/Berlin"); /* -> 13:40:37 (German format, current time in Berlin) */ /* You may also use toLocaleString instead of toLocaleTimeString, which will give you the time AND the date */

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.