From 1e43dc7b89bb48cc7770a6c38d9495accbb2ed4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Danic=CC=81?= Date: Wed, 17 Oct 2018 11:36:41 +0200 Subject: [PATCH 1/4] Improve the readability of tomorrow.js (solved #775) --- snippets/tomorrow.md | 14 ++++++-------- test/tomorrow/tomorrow.js | 8 +++----- test/tomorrow/tomorrow.test.js | 4 ++++ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/snippets/tomorrow.md b/snippets/tomorrow.md index 9d9300071..8288d2f08 100644 --- a/snippets/tomorrow.md +++ b/snippets/tomorrow.md @@ -2,20 +2,18 @@ Results in a string representation of tomorrow's date. -Use `new Date()` to get today's date, adding one day using `Date.getDate()` and `Date.setDate()`, and converting the Date object to a string. +First we use `new Date()` to get today's date, then add one day using `Date.getDate()` and mutate the initial `Date` via `Date.setDate()`. The we use `.toISOString` to get the date part of the string – discarding the time part. ```js -const tomorrow = (long = false) => { +const tomorrow = () => { let t = new Date(); t.setDate(t.getDate() + 1); - const ret = `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String( - t.getDate() - ).padStart(2, '0')}`; - return !long ? ret : `${ret}T00:00:00`; + return t.toISOString().split('T')[0]; }; ``` ```js -tomorrow(); // 2017-12-27 (if current date is 2017-12-26) -tomorrow(true); // 2017-12-27T00:00:00 (if current date is 2017-12-26) +tomorrow(); // 2018-10-18 (if current date is 2018-10-18) +// if you need the time to indicate the start of the day as well: +`${tomorrow()}T00:00:00}: // 2018-10-18T00:00:00 ``` diff --git a/test/tomorrow/tomorrow.js b/test/tomorrow/tomorrow.js index a78e50b47..9ff554de4 100644 --- a/test/tomorrow/tomorrow.js +++ b/test/tomorrow/tomorrow.js @@ -1,9 +1,7 @@ -const tomorrow = (long = false) => { +const tomorrow = () => { let t = new Date(); t.setDate(t.getDate() + 1); - const ret = `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String( - t.getDate() - ).padStart(2, '0')}`; - return !long ? ret : `${ret}T00:00:00`; + return t.toISOString().split('T')[0]; }; + module.exports = tomorrow; diff --git a/test/tomorrow/tomorrow.test.js b/test/tomorrow/tomorrow.test.js index 4827289c8..e8dfe1a03 100644 --- a/test/tomorrow/tomorrow.test.js +++ b/test/tomorrow/tomorrow.test.js @@ -4,6 +4,10 @@ const tomorrow = require('./tomorrow.js'); test('tomorrow is a Function', () => { expect(tomorrow).toBeInstanceOf(Function); }); +test('Returns the correct type', () => { + expect(typeof tomorrow()).toBe('string'); +}); + const t1 = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 1); const t2 = new Date(tomorrow()); test('Returns the correct year', () => { From ab44e065d9780e86400785cc2dbeb0452e4cba54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Danic=CC=81?= Date: Wed, 17 Oct 2018 21:37:54 +0200 Subject: [PATCH 2/4] revert to previous implementation, but make it cleaner --- test/tomorrow/tomorrow.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/tomorrow/tomorrow.js b/test/tomorrow/tomorrow.js index 9ff554de4..c061ad181 100644 --- a/test/tomorrow/tomorrow.js +++ b/test/tomorrow/tomorrow.js @@ -1,7 +1,10 @@ const tomorrow = () => { - let t = new Date(); + const t = new Date(); t.setDate(t.getDate() + 1); - return t.toISOString().split('T')[0]; + const year = t.getFullYear(); + const month = String(t.getMonth() + 1).padStart(2, '0'); + const date = String(t.getDate()).padStart(2, '0'); + return `${year}-${month}-${date}`; }; module.exports = tomorrow; From bc575fea573e0a325252842edea27845f133345e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Danic=CC=81?= Date: Wed, 17 Oct 2018 21:50:11 +0200 Subject: [PATCH 3/4] Update tomorrow.md --- snippets/tomorrow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/tomorrow.md b/snippets/tomorrow.md index 8288d2f08..1027c84ef 100644 --- a/snippets/tomorrow.md +++ b/snippets/tomorrow.md @@ -2,7 +2,7 @@ Results in a string representation of tomorrow's date. -First we use `new Date()` to get today's date, then add one day using `Date.getDate()` and mutate the initial `Date` via `Date.setDate()`. The we use `.toISOString` to get the date part of the string – discarding the time part. +First we use `new Date()` to get today's date, then add one day using `Date.getDate()` and mutate the initial `Date` via `Date.setDate()`. Then we use construct the date string in `yyyy-mm-dd` format. ```js const tomorrow = () => { From d0436d620f45195ff82a4fcc2844f84151537ee4 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 9 Jan 2019 19:21:24 +0200 Subject: [PATCH 4/4] Final cleanup of tomorrow snippet --- snippets/tomorrow.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/tomorrow.md b/snippets/tomorrow.md index 1027c84ef..0d16b6bbf 100644 --- a/snippets/tomorrow.md +++ b/snippets/tomorrow.md @@ -2,7 +2,8 @@ Results in a string representation of tomorrow's date. -First we use `new Date()` to get today's date, then add one day using `Date.getDate()` and mutate the initial `Date` via `Date.setDate()`. Then we use construct the date string in `yyyy-mm-dd` format. +Use `new Date()` to get the current date, increment by one using `Date.getDate()` and set the value to the result using `Date.setDate()`. +Use `Date.prototype.toISOString()` to return a string in `yyyy-mm-dd` format. ```js const tomorrow = () => { @@ -14,6 +15,4 @@ const tomorrow = () => { ```js tomorrow(); // 2018-10-18 (if current date is 2018-10-18) -// if you need the time to indicate the start of the day as well: -`${tomorrow()}T00:00:00}: // 2018-10-18T00:00:00 ```