-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathscript.js
121 lines (102 loc) · 4.71 KB
/
script.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
document.addEventListener('DOMContentLoaded', function () {
const calculator = document.getElementById('calculator');
const unitSelect = document.getElementById('unit');
const timeInput = document.getElementById('time');
const dateInput = document.getElementById('date');
const calculateButton = document.getElementById('calculate');
const timeRemaining = document.getElementById('time-remaining');
const mode = document.getElementById('mode');
const modeToggle = document.getElementById('mode-toggle');
const direction = document.getElementById('direction');
const currentDateDisplay = document.getElementById('current-date-display');
const currentTimeDisplay = document.getElementById('current-time-display');
// Function to update the current date and time
function updateCurrentDateTime() {
const currentTime = new Date();
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = currentTime.toLocaleString("en-UK", options);
const formattedTime = currentTime.toLocaleTimeString('en-US', {
hour12: false,
});
currentDateDisplay.textContent = formattedDate;
currentTimeDisplay.textContent = formattedTime;
const currentDate = new Date();
}
// Update the current date and time initially
updateCurrentDateTime();
// Set up a timer to update the current date and time every second
setInterval(updateCurrentDateTime, 1000);
// Function to toggle the visibility of the time input based on the selected unit
function toggleTimeInputVisibility() {
const selectedUnit = unitSelect.value;
const dateInput = document.getElementById('dateInput');
const timeInput = document.getElementById('timeInput');
if (selectedUnit === 'hours') {
timeInput.style.display = 'block';
dateInput.style.display = 'none';
} else {
timeInput.style.display = 'none';
dateInput.style.display = 'block';
}
}
// Initial call to set the initial state
toggleTimeInputVisibility();
// Add an event listener to the unit select to update the visibility when the selection changes
unitSelect.addEventListener('change', toggleTimeInputVisibility);
function calculateTimeRemaining() {
const selectedUnit = unitSelect.value;
const currentDateTime = new Date();
let inputDateTime;
if (selectedUnit === 'hours') {
const timeParts = timeInput.value.split(':');
inputDateTime = new Date(currentDateTime);
inputDateTime.setHours(parseInt(timeParts[0]));
inputDateTime.setMinutes(parseInt(timeParts[1]));
} else {
inputDateTime = new Date(dateInput.value);
}
const timeDifference = inputDateTime - currentDateTime;
timeDifference < 0
? (direction.textContent = 'Passed')
: (direction.textContent = 'Remaining');
absTimeDifference = Math.abs(timeDifference);
if (selectedUnit === 'hours') {
const hours = Math.floor(absTimeDifference / (1000 * 60 * 60));
const minutes = Math.floor((absTimeDifference / (1000 * 60)) % 60);
timeRemaining.textContent = `${hours} hours and ${minutes} minutes`;
} else if (selectedUnit === 'days') {
const days = Math.floor(absTimeDifference / (1000 * 60 * 60 * 24));
timeRemaining.textContent = `${days} days`;
} else {
const years = Math.floor(
absTimeDifference / (1000 * 60 * 60 * 24 * 365)
);
const remainingTimeInMonths = Math.floor(
((absTimeDifference / (1000 * 60 * 60 * 24)) % 365) / 30
);
const remainingTimeInDays = Math.floor(
(absTimeDifference / (1000 * 60 * 60 * 24)) % 30
);
timeRemaining.textContent = `${years} years and ${remainingTimeInMonths} months and ${remainingTimeInDays} days`;
}
}
// Add an event listener to the calculate button
calculateButton.addEventListener('click', calculateTimeRemaining);
timeInput.addEventListener('input', hideCalendar);
dateInput.addEventListener('input', hideCalendar);
function hideCalendar() {
if (timeInput.value || dateInput.value) {
timeInput.blur();
dateInput.blur();
}
}
modeToggle.addEventListener('change', () => {
document.body.classList.toggle('light-mode');
calculator.classList.toggle('light-mode');
if (document.body.classList.contains('light-mode')) {
mode.innerText = 'Dark';
} else {
mode.innerText = 'Light';
}
});
});