JavaScript Format Date and Time (based on locale)

Oct 24, 2019

Date

const options = { year: 'numeric', month: 'short', day: 'numeric' };const date = new Date()date.toLocaleDateString(undefined, options)     // default localedate.toLocaleDateString('en-US', options)       // Oct 24, 2019date.toLocaleDateString('zh-CN', options)       // 2019年10月24日

NOTE: Date.toLocaleDateString

Time

const options = { hour: '2-digit', minute: '2-digit' }const date = new Date()date.toLocaleTimeString()                   // "1:09:07 PM" - Depending on System Locale Settingsdate.toLocaleString(undefined, options)     // "01:09 PM"

NOTE: Date.toLocaleTimeString

Date and Time

const options = { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }const date = new Date()date.toLocaleDateString()                   // "3/28/2020" - Depending on System Locale Settingsdate.toLocaleString(undefined, options)     // "Mar 28, 2020, 01:09 PM"

NOTE: Date.toLocaleString

❤️ 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.