Improve the readability of tomorrow.js (solved #775)
This commit is contained in:
@ -2,20 +2,18 @@
|
|||||||
|
|
||||||
Results in a string representation of tomorrow's date.
|
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
|
```js
|
||||||
const tomorrow = (long = false) => {
|
const tomorrow = () => {
|
||||||
let t = new Date();
|
let t = new Date();
|
||||||
t.setDate(t.getDate() + 1);
|
t.setDate(t.getDate() + 1);
|
||||||
const ret = `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String(
|
return t.toISOString().split('T')[0];
|
||||||
t.getDate()
|
|
||||||
).padStart(2, '0')}`;
|
|
||||||
return !long ? ret : `${ret}T00:00:00`;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
|
tomorrow(); // 2018-10-18 (if current date is 2018-10-18)
|
||||||
tomorrow(true); // 2017-12-27T00:00:00 (if current date is 2017-12-26)
|
// if you need the time to indicate the start of the day as well:
|
||||||
|
`${tomorrow()}T00:00:00}: // 2018-10-18T00:00:00
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
const tomorrow = (long = false) => {
|
const tomorrow = () => {
|
||||||
let t = new Date();
|
let t = new Date();
|
||||||
t.setDate(t.getDate() + 1);
|
t.setDate(t.getDate() + 1);
|
||||||
const ret = `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String(
|
return t.toISOString().split('T')[0];
|
||||||
t.getDate()
|
|
||||||
).padStart(2, '0')}`;
|
|
||||||
return !long ? ret : `${ret}T00:00:00`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = tomorrow;
|
module.exports = tomorrow;
|
||||||
|
|||||||
@ -4,6 +4,10 @@ const tomorrow = require('./tomorrow.js');
|
|||||||
test('tomorrow is a Function', () => {
|
test('tomorrow is a Function', () => {
|
||||||
expect(tomorrow).toBeInstanceOf(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 t1 = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 1);
|
||||||
const t2 = new Date(tomorrow());
|
const t2 = new Date(tomorrow());
|
||||||
test('Returns the correct year', () => {
|
test('Returns the correct year', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user