Start by reading the supported values as part of the Intl Web/JS APi and checking what's available => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/supportedValuesOf . Anything that's returned in the list you can use in your code/app and browser - but may not work in other clients and browsers.
Unless you dig deep into the topic of time I wouldn't try to convert time that a user has entered from a new location selected in their profile - this could be complicated. Better to let the user pick a location and let the location set the time then save that. Calculation back and off of the time? No need - just use Int API to read from.
That said have a try at my code using no external libraries:
- User logs in and is based in Pakistan or wherever
- User then changes the timezone to Singapore or somewhere else - this is done in this section of code
let userSelectedCountry = 'Asia/Singapore'
and call our function by using the Date and RegEx APIs to achieve what we need and pull out the correct time for selected (available timezone in our array!) timezone
To use it follow step 1 and 2 in the code comments.
const event = new Date();
//list of locales - NOT exhaustive
const localeCodes = ['el', 'en-au', 'en-ca', 'en-gb', 'en-us', 'es-es', 'es-mx','it-it']
//list of timezones - NOT exhaustive
const timeZonesL = ["Africa/Abidjan"
, "Asia/Aden"
, "Asia/Almaty"
, "Asia/Amman"
, "Asia/Anadyr"
, "Asia/Aqtau"
, "Asia/Aqtobe"
, "Asia/Ashgabat"
, "Asia/Ashkhabad"
, "Asia/Atyrau"
, "Asia/Baghdad"
, "Asia/Bahrain"
, "Asia/Baku"
, "Asia/Bangkok"
, "Asia/Barnaul"
, "Asia/Beirut"
, "Asia/Bishkek"
, "Asia/Brunei"
, "Asia/Calcutta"
, "Asia/Chita"
, "Asia/Choibalsan"
, "Asia/Chongqing"
, "Asia/Chungking"
, "Asia/Colombo"
, "Asia/Dacca"
, "Asia/Damascus"
, "Asia/Dhaka"
, "Asia/Dili"
, "Asia/Dubai"
, "Asia/Dushanbe"
, "Asia/Famagusta"
, "Asia/Gaza"
, "Asia/Harbin"
, "Asia/Hebron"
, "Asia/Ho_Chi_Minh"
, "Asia/Hong_Kong"
, "Asia/Hovd"
, "Asia/Irkutsk"
, "Asia/Istanbul"
, "Asia/Jakarta"
, "Asia/Jayapura"
, "Asia/Jerusalem"
, "Asia/Kabul"
, "Asia/Kamchatka"
, "Asia/Karachi"
, "Asia/Kashgar"
, "Asia/Kathmandu"
, "Asia/Katmandu"
, "Asia/Khandyga"
, "Asia/Kolkata"
, "Asia/Krasnoyarsk"
, "Asia/Kuala_Lumpur"
, "Asia/Kuching"
, "Asia/Kuwait"
, "Asia/Macao"
, "Asia/Macau"
, "Asia/Magadan"
, "Asia/Makassar"
, "Asia/Manila"
, "Asia/Muscat"
, "Asia/Nicosia"
, "Asia/Novokuznetsk"
, "Asia/Novosibirsk"
, "Asia/Omsk"
, "Asia/Oral"
, "Asia/Phnom_Penh"
, "Asia/Pontianak"
, "Asia/Pyongyang"
, "Asia/Qatar"
, "Asia/Qostanay"
, "Asia/Qyzylorda"
, "Asia/Rangoon"
, "Asia/Riyadh"
, "Asia/Saigon"
, "Asia/Sakhalin"
, "Asia/Samarkand"
, "Asia/Seoul"
, "Asia/Shanghai"
, "Asia/Singapore"
, "Asia/Srednekolymsk"
, "Asia/Taipei"
, "Asia/Tashkent"
, "Asia/Tbilisi"
, "Asia/Tehran"
, "Asia/Tel_Aviv"
, "Asia/Thimbu"
, "Asia/Thimphu"
, "Asia/Tokyo"
, "Asia/Tomsk"
, "Asia/Ujung_Pandang"
, "Asia/Ulaanbaatar"
, "Asia/Ulan_Bator"
, "Asia/Urumqi"
, "Asia/Ust-Nera"
, "Asia/Vientiane"
, "Asia/Vladivostok"
, "Asia/Yakutsk"
, "Asia/Yangon"
, "Asia/Yekaterinburg"
, "Asia/Yerevan"
, "Europe/London"]
getDateTime = (country) => {
const regexp = new RegExp(`\\b${country}\\b`, 'gi') // showing how to pass optional flags
const userTimeZone = timeZonesL.filter(timeZ => timeZ == timeZ.match(country))
return event.toLocaleString('en-GB', { timeZone: userTimeZone });
//return event.toLocaleString('en-GB', { timeZone: 'Asia/Singapore' });;
}
//step 1
//test 1 - after user signs in and selects new country/timezone we call it for them
//e.g. from a dropdown
//Value in drop down must match a value in the timeZonesL array
let userSelectedCountry = 'Asia/Singapore'
//build regex and pass in selected country
var re = new RegExp("\\b"+userSelectedCountry+"\\b");
//we then call our function with it as a param if it tests truthy to regex match
console.log(getDateTime(userSelectedCountry.match(re)))
//step 2
//On sign out save the time as to what the user selected e.g.
//mongoDB.save(getDateTime(userSelectedCountry.match(re)))
//now we can save any time the user selects
//NOTE: more needs to be done to finalise list of timezones as some are redundant, not supported by some
//browsers and you don't want all of them