I need a JavaScript-function which always computes the date of the next day. Then returns the date as a string, formatted in the way: dd.mm.yyyy
I wrote this code:
const getDateStringForTomorrow = () => {
const millisOfDay = 1000 * 60 * 60 * 24;
const oTomorrow = new Date(Date.now() + millisOfDay);
const day = ("0" + oTomorrow.getDate()).slice(-2);
const month = ("0" + (oTomorrow.getMonth() + 1)).slice(-2);
const year = oTomorrow.getFullYear();
return `${day}.${month}.${year}`;
};
console.log(getDateStringForTomorrow());
Can I expect my function to work as expected and to provide correct results?
What't your opinion about the way I have written the function? To you think it's overly verbose?
.toString().padStart(2, '0')
- I think it reads a little better. \$\endgroup\$oTomorrow = new Date(); oTomorrow.setDate(oTomorrow.getDate() + 1)
(setDate() will correctly handle too-large numbers) \$\endgroup\$