Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 587c6dc

Browse files
The Date Object in JS ✨🍟
1 parent ceea36c commit 587c6dc

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Hello World!</title>
7+
</head>
8+
<body>
9+
10+
<script src="./script.js"></script>
11+
12+
</body>
13+
</html>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
document.write("The Date Object in JavaScript!" + "<br /><br />");
2+
3+
/*
4+
The Date Object
5+
6+
*/
7+
8+
let myDate = new Date(); // Getting Current Date.
9+
console.log("Current Date : " + myDate);
10+
11+
// Getting Past Date
12+
let myPastDate = new Date(1545, 11, 2); // Month is 0 to 11. 0 means january and 11 means decemeber wise.
13+
console.log("Past Date : " + myPastDate);
14+
15+
// Getting Future Date
16+
let myFutureDate = new Date(2515, 0, 31); // Month is 0 to 11. 0 means january and 11 means decemeber wise.
17+
console.log("Future Date : " + myFutureDate);
18+
19+
20+
21+
// Calling methods from date object
22+
let birthday = new Date(1985, 0, 15, 11, 15, 25); // (YYYY, MM, DD, HH, mm, ss)
23+
console.log("Birthday : " + birthday);
24+
25+
console.log("Birthday Month : " + birthday.getMonth()); // get the month of the date (0 - 11)
26+
console.log("Birthday Year : " + birthday.getFullYear()); // get the full year (YYYY)
27+
console.log("Birthday Date : " + birthday.getDate()); // get the date of the month (0 - 31)
28+
console.log("Birthday Day : " + birthday.getDay()); // get the day of week (0 -6)
29+
console.log("Birthday Hour : " + birthday.getHours()); // get the hour of the date (0 - 23)
30+
console.log("Birthday Time : " + birthday.getTime()); // get the number of miliseconds since 1st jan 1970
31+
32+
// Why we need getTime() method?
33+
// lets make another date as birthday2, but values same.
34+
let birthday2 = new Date(1985, 0, 15, 11, 15, 25);
35+
36+
// Lets create if else statement to check this birthday and birthday2 objects are same or not
37+
if ( birthday === birthday2){
38+
39+
console.log("Birthdays are equal");
40+
41+
} else {
42+
43+
console.log("Birthdays are not equal");
44+
45+
}
46+
// This if-else gives "Birthdays are not equal" statement. but these two birthdays have same values then why?
47+
// That because birthday and birthday2 are objects of Date() object are not same thats why throwing not equal satement. Then how we check these values of birthdays.
48+
49+
if( birthday.getTime() === birthday2.getTime() ){
50+
51+
console.log("Birthdays are equal");
52+
53+
} else {
54+
55+
console.log("Birthdays are not equal");
56+
57+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /