Last active
November 8, 2022 10:22
-
-
Save dazecoop/049d8d38d6481d83803399368de0f9c0 to your computer and use it in GitHub Desktop.
Vanilla JS full current date, with no dependencies & has localization support. One-liner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Formatted in D MMMM YYYY | |
// Eg; 18 October 2022 | |
`${(new Date().getDate())} ${(new Date().toLocaleString("en", { month: "long" }))} ${(new Date().getFullYear())}` | |
// Formatted in D{ordinal} MMMM YYYY | |
// Eg; 18th October 2022 | |
`${(new Date().getDate())}${{one:'st',two:'nd',few:'rd',other:'th'}[new Intl.PluralRules('en-GB', { type: 'ordinal' }).select(new Date().getDate())]} ${(new Date().toLocaleString("en", { month: "long" }))} ${(new Date().getFullYear())}` | |
// Formatted in DD-MM-YYYY | |
// Eg; 18-09-2022 | |
`${String(new Date().getDate()).padStart(2,0)}-${String(new Date().getMonth()+1).padStart(2,0)}-${(new Date().getFullYear())}` | |
// Formatted {Day of week} | |
// Eg; Tuesday | |
`(new Date()).toLocaleString("en", { weekday: "long" })` | |
// ** | |
// Subtract X-days from current date, then output | |
var d = new Date(); d.setDate(d.getDate()-5); | |
// ... Followed by your desired output, replacing `new Date()` with `d`. Eg; | |
`${String(d.getDate()).padStart(2,0)}-${String(d.getMonth()+1).padStart(2,0)}-${(d.getFullYear())}` | |
// eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment