Rácz Tibor Zoltán Posted on Mar 19, 2020 JavaScript format Date as YYYY-MM-DD #javascript #date #format A simple and awesome method: yourDate.toISOString().split('T')[0] But be careful! This result based on UTC date and time. Top comments (3) Subscribe Submit Preview Dismiss Collapse Expand Pacharapol Withayasakpunt Pacharapol Withayasakpunt Pacharapol Withayasakpunt Follow Currently interested in TypeScript, Vue, Kotlin and Python. Looking forward to learning DevOps, though. Location Thailand Education Yes Joined Oct 30, 2019 • Mar 19 '20 • Edited on Mar 19 • Edited Dropdown menu Copy link Hide To get locale date and time, use new Date().getTimezoneOffset() in minutes, so it becomes function getYYYYMMDD(d0) { const d = new Date(d0) return new Date(d.getTime() - d.getTimezoneOffset() * 60 * 1000).toISOString().split('T')[0] } Enter fullscreen mode Exit fullscreen mode Collapse Expand Mugilan Mugilan Mugilan Follow A Full Stack Javascript Developer Email mugilancodes@gmail.com Location Chennai, Tamil Nadu, India Joined Jul 19, 2020 • Jan 16 '21 Dropdown menu Copy link Hide I prefer this const today = new Date().toISOString().slice(0, 10) console.log(today) // 2021-01-16 Enter fullscreen mode Exit fullscreen mode Collapse Expand Jayro_Salazar Jayro_Salazar Jayro_Salazar Follow Joined Sep 6, 2020 • Dec 8 '20 Dropdown menu Copy link Hide It work, thanks dut Code of Conduct • Report abuse
Top comments (3)
To get locale date and time, use
new Date().getTimezoneOffset()in minutes, so it becomesI prefer this
It work, thanks dut