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