728x90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDateAgo(date, days) { | |
const copiedDate = new Date(date); | |
copiedDate.setDate(copiedDate.getDate() - days); | |
return copiedDate; | |
} | |
let date = new Date(2015, 0, 2); | |
console.log( getDateAgo(date, 1).getDate() ); // 1, (1 Jan 2015) | |
console.log( getDateAgo(date, 2).getDate() ); // 31, (31 Dec 2014) | |
console.log( getDateAgo(date, 365).getDate() ); // 2, (2 Jan 2014) | |
console.log( getDateAgo(date, 1) ); | |
console.log( getDateAgo(date, 2) ); | |
console.log( getDateAgo(date, 365) ); |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: Date and Time: Last day of month (0) | 2021.12.02 |
---|---|
The Modern JS Tutorial: Date and Time: How many seconds have passed today (0) | 2021.12.02 |
The Modern JS Tutorial: Date and Time: European weekday (0) | 2021.11.29 |
The Modern JS Tutorial: Date and Time: show a weekday (0) | 2021.11.17 |
The Modern JS Tutorial: Date and Time: create a date (0) | 2021.11.15 |