-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecalc.js
More file actions
71 lines (57 loc) · 2.86 KB
/
Copy pathtimecalc.js
File metadata and controls
71 lines (57 loc) · 2.86 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
document.addEventListener('DOMContentLoaded', function() {
const input1 = document.getElementById('start');
const input2 = document.getElementById('end');
const addButton = document.getElementById('submitB');
const differenceResultDisplay = document.getElementById('differenceResult');
const addMinutesButton = document.getElementById('addMinutesButton');
const addResultDisplay = document.getElementById('addResult');
addButton.addEventListener('click', function() {
const time1 = input1.value.split(':');
const time2 = input2.value.split(':');
if (time1.length === 2 && time2.length === 2) {
const hours1 = parseInt(time1[0]);
const minutes1 = parseInt(time1[1]);
const hours2 = parseInt(time2[0]);
const minutes2 = parseInt(time2[1]);
if (!isNaN(hours1) && !isNaN(minutes1) && !isNaN(hours2) && !isNaN(minutes2)) {
const totalMinutes1 = hours1 * 60 + minutes1;
const totalMinutes2 = hours2 * 60 + minutes2;
let timeDifference = totalMinutes2 - totalMinutes1;
if (timeDifference < 0) {
timeDifference += 24 * 60;
}
const resultHours = Math.floor(timeDifference / 60);
const resultMinutes = timeDifference % 60;
differenceResultDisplay.textContent = `Time Difference: ${resultHours} hours ${resultMinutes} minutes`;
} else {
differenceResultDisplay.textContent = 'Invalid input';
}
} else {
differenceResultDisplay.textContent = 'Invalid time format';
}
});
addMinutesButton.addEventListener('click', function() {
const timeInput = document.getElementById("timeInput").value;
const minutesInput = parseInt(document.getElementById("minutesInput").value, 10);
if (isNaN(minutesInput)) {
alert("Please enter a valid number of minutes.");
return;
}
const timeParts = timeInput.split(":");
if (timeParts.length !== 2) {
alert("Please enter a valid time in the format HH:MM.");
return;
}
const hours = parseInt(timeParts[0]);
const minutes = parseInt(timeParts[1]);
if (isNaN(hours) || isNaN(minutes)) {
alert("Please enter a valid time in the format HH:MM.");
return;
}
let totalMinutes = hours * 60 + minutes + minutesInput;
totalMinutes = totalMinutes % (24 * 60); // Wrap around 24 hours
const newHours = Math.floor(totalMinutes / 60);
const newMinutes = totalMinutes % 60;
addResultDisplay.textContent = `New time: ${String(newHours).padStart(2, '0')}:${String(newMinutes).padStart(2, '0')}`;
});
});