Javascript Format Time to String (based on locale)

Nov 28, 2019
const date = new Date()date.toLocaleTimeString()           // 6:01:56 PMdate.toLocaleTimeString('zh-CN')    // 下午6:01:56"// use default locale with options// only 'UTC' is guranteed to work across browser,// other IANA time zones (e.g. America/New_York) does not work in IE, Safari, Firebox Android, etc.date.toLocaleTimeString(undefined, {timeZone: 'UTC'})   // 10:01:56 AMdate.toLocaleTimeString('en-US', {timeStyle: 'full'})   // 6:01:56 PM Malaysia Timedate.toLocaleTimeString('en-US', {timeStyle: 'long'})   // 6:01:56 PM GMT+8date.toLocaleTimeString('en-US', {timeStyle: 'medium'}) // 6:01:56 PMdate.toLocaleTimeString('en-US', {timeStyle: 'short'})  // 6:01 PMdate.toLocaleTimeString('en-US', {timeStyle: 'short', hour12: false})   // 18:01date.toLocaleTimeString('en-US', {hour: 'numeric'})     // 6 PMdate.toLocaleTimeString('en-US', {hour: '2-digit'})     // 06 PM

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.