-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path56 Date().html
75 lines (67 loc) · 2.06 KB
/
56 Date().html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JavaScript Date()</title>
</head>
<body>
<script>
//Using new Date()
const currDate = new Date();
//new Date() without arguments, creates a date object with the current date and time
console.log("current date and time:",currDate);
//The getFullYear() Method
console.log("current year:",currDate.getFullYear()); //Return the full year of a date object
//getMonth()
console.log("current month:",currDate.getMonth()); //Return the month as a number: 0=>January, 1=>February,...
//The getDay() Method
console.log("current day",currDate.getDay()); //Return the day as a number: 0->sunday, 1->monday,....
//The toLocaleTimeString() method
console.log("current local time:",currDate.toLocaleTimeString());
//displaying day
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = days[currDate.getDay()]; //or let day = days[new Date().getDay()];
console.log("Today is: ", day);
//or use switch statement
let day_;
switch (new Date().getDay()) {
case 0:
day_ = "Sunday";
break;
case 1:
day_ = "Monday";
break;
case 2:
day_ = "Tuesday";
break;
case 3:
day_ = "Wednesday";
break;
case 4:
day_ = "Thursday";
break;
case 5:
day_ = "Friday";
break;
case 6:
day_ = "Saturday";
}
console.log(day_);
//get hours
const hours = new Date().getHours();
console.log("current hour:",hours);
console.log("current minute:",new Date().getMinutes())
console.log("current second:",new Date().getSeconds())
</script>
</body>
</html>