-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.html
More file actions
54 lines (46 loc) · 1.9 KB
/
date.html
File metadata and controls
54 lines (46 loc) · 1.9 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Date Methods Example</title>
</head>
<body>
<h1>Date Methods Example</h1>
<p>Open the browser console to see the results.</p>
<div id="output">
<!-- Output variations will be displayed here -->
</div>
<script>
const outputDiv = document.getElementById("output");
// Current date and time
const currentDate = new Date();
const currentDateString = `<p>Current Date: ${currentDate}</p>`;
outputDiv.innerHTML += currentDateString;
// Get specific components of the date
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // Months are 0-based
const day = currentDate.getDate();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const dateComponentsString = `<p>Year: ${year}, Month: ${month}, Day: ${day}, Hours: ${hours}, Minutes: ${minutes}</p>`;
outputDiv.innerHTML += dateComponentsString;
// Formatting the date
const formattedDate = currentDate.toLocaleDateString("en-US");
const formattedTime = currentDate.toLocaleTimeString("en-US");
const formattedDateString = `<p>Formatted Date: ${formattedDate}, Formatted Time: ${formattedTime}</p>`;
outputDiv.innerHTML += formattedDateString;
// Getting the day of the week
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const dayOfWeek = daysOfWeek[currentDate.getDay()];
const dayOfWeekString = `<p>Day of the Week: ${dayOfWeek}</p>`;
outputDiv.innerHTML += dayOfWeekString;
// Adding days to the current date
const daysToAdd = 5;
const futureDate = new Date(currentDate);
futureDate.setDate(futureDate.getDate() + daysToAdd);
const futureDateString = `<p>Date after adding ${daysToAdd} days: ${futureDate}</p>`;
outputDiv.innerHTML += futureDateString;
</script>
</body>
</html>