diff --git a/README.md b/README.md index ace0d83..9db6319 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/javascript/basic-date-formatting-without-a-library.md b/javascript/basic-date-formatting-without-a-library.md new file mode 100644 index 0000000..05daa6c --- /dev/null +++ b/javascript/basic-date-formatting-without-a-library.md @@ -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.