1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00

Add Basic Date Formatting Without A Library as a javascript til

This commit is contained in:
jbranchaud
2019-11-19 16:33:42 -06:00
parent bd41a371b1
commit d0eebf8bed
2 changed files with 41 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
_869 TILs and counting..._
_870 TILs and counting..._
---
@@ -274,6 +274,7 @@ _869 TILs and counting..._
### JavaScript
- [Accessing Arguments To A Function](javascript/accessing-arguments-to-a-function.md)
- [Basic Date Formatting Without A Library](javascript/basic-date-formatting-without-a-library.md)
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
- [Check The Password Confirmation With Yup](javascript/check-the-password-confirmation-with-yup.md)

View File

@@ -0,0 +1,39 @@
# Basic Date Formatting Without A Library
JavaScript, on modern browsers, has an Internationalization API that, among
other things, provides some date formatting utilities. You can just start using
it, no need to import some massive date formatting library.
Here is a `Date` object:
```javascript
> const now = new Date();
> now
Tue Nov 19 2019 16:23:43 GMT-0600 (Central Standard Time)
```
The default formatting with this API is a good start:
```javascript
> Intl.DateTimeFormat('en-US').format(now)
"11/19/2019"
```
There are also a number of options for more advanced formatting. Here is the
`dateStyle` option with the four possible option values:
```javascript
> Intl.DateTimeFormat('en-US', { dateStyle: "full" }).format(now)
"Tuesday, November 19, 2019"
> Intl.DateTimeFormat('en-US', { dateStyle: "long" }).format(now)
"November 19, 2019"
> Intl.DateTimeFormat('en-US', { dateStyle: "medium" }).format(now)
"Nov 19, 2019"
> Intl.DateTimeFormat('en-US', { dateStyle: "short" }).format(now)
"11/19/19"
```
There is a lot more to this API including localization and custom formatting.
Check out the
[docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat)
for those details.