Skip to content

Commit f58b66a

Browse files
committed
[Add] Dates in javascript
1 parent 7bc224b commit f58b66a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Creating a new Date object
2+
const currentDate = new Date();
3+
console.log("Current Date:", currentDate);
4+
5+
// Getting the current date components
6+
const day = currentDate.getDate();
7+
const month = currentDate.getMonth() + 1; // Months are zero-based, so we add 1
8+
const year = currentDate.getFullYear();
9+
console.log("Day:", day);
10+
console.log("Month:", month);
11+
console.log("Year:", year);
12+
13+
// Formatting the date
14+
const formattedDate = `${day}/${month}/${year}`;
15+
console.log("Formatted Date:", formattedDate);
16+
17+
// Adding days to a date
18+
const futureDate = new Date(currentDate);
19+
futureDate.setDate(futureDate.getDate() + 7); // Adding 7 days
20+
console.log("Future Date:", futureDate);
21+
22+
// Subtracting days from a date
23+
const pastDate = new Date(currentDate);
24+
pastDate.setDate(pastDate.getDate() - 3); // Subtracting 3 days
25+
console.log("Past Date:", pastDate);
26+
27+
// Comparing dates
28+
const isFutureDate = futureDate > currentDate;
29+
const isPastDate = pastDate < currentDate;
30+
console.log("Is Future Date:", isFutureDate);
31+
console.log("Is Past Date:", isPastDate);

0 commit comments

Comments
 (0)