diff --git a/README.md b/README.md index 57cf521..df4ee77 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). -_969 TILs and counting..._ +_970 TILs and counting..._ --- @@ -324,6 +324,7 @@ _969 TILs and counting..._ - [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md) - [Create An Object With No Properties](javascript/create-an-object-with-no-properties.md) - [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md) +- [Create Future And Past Dates From Today](javascript/create-future-and-past-dates-from-today.md) - [Custom Type Checking Error Messages With Yup](javascript/custom-type-checking-error-messages-with-yup.md) - [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md) - [Define A Custom Jest Matcher](javascript/define-a-custom-jest-matcher.md) diff --git a/javascript/create-future-and-past-dates-from-today.md b/javascript/create-future-and-past-dates-from-today.md new file mode 100644 index 0000000..bc1ba74 --- /dev/null +++ b/javascript/create-future-and-past-dates-from-today.md @@ -0,0 +1,34 @@ +# Create Future And Past Dates From Today + +JavaScript's built-in +[`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) +object can be frustrating to work with at times. It does, however, lend itself +nicely to some date math. You have to familiarize yourself with some of the API +and then it is a matter of addition and subtraction. + +Here is today: + +```javascript +const today = new Date(); +// Tue Dec 01 2020 ... +``` + +Let's make a copy of today and send it 30 days into the future: + +```javascript +const future = new Date(today); +future.setDate(future.getDate() + 30); +future +// Thu Dec 31 2020 ... +``` + +Or we could jump back a few years: + +```javascript +const past = new Date(today); +past.setFullYear(past.getFullYear() - 4); +past +// Thu Dec 01 2016 ... +``` + +[source](https://stackoverflow.com/questions/7908098/javascript-set-date-30-days-from-now/7908122#7908122)