-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
150 lines (130 loc) · 5.49 KB
/
scripts.js
File metadata and controls
150 lines (130 loc) · 5.49 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const tabNames = ["datediff", "timezone", "countdown", "age", "timestamp"];
function showTool(index) {
const tabs = document.querySelectorAll('.tab');
const tools = document.querySelectorAll('.tool');
tabs.forEach((tab, i) => tab.classList.toggle('active', i === index));
tools.forEach((tool, i) => tool.classList.toggle('active', i === index));
if (tabNames[index]) {
window.location.hash = tabNames[index];
}
}
function calculateDifference() {
const date1 = new Date(document.getElementById("date1").value);
const date2 = new Date(document.getElementById("date2").value);
const unit = document.getElementById("unit").value;
if (!date1 || !date2) return;
const diffMs = Math.abs(date2 - date1);
let result;
switch (unit) {
case "days": result = diffMs / (1000 * 60 * 60 * 24); break;
case "hours": result = diffMs / (1000 * 60 * 60); break;
case "minutes": result = diffMs / (1000 * 60); break;
case "seconds": result = diffMs / 1000; break;
}
document.getElementById("diffResult").textContent = `Difference: ${result.toFixed(2)} ${unit}`;
}
function convertTimezone() {
const source = document.getElementById("sourceDate").value;
const fromTZ = document.getElementById("fromTZ").value;
const toTZ = document.getElementById("toTZ").value;
if (!source) return;
const dt = new Date(source);
const utc = dt.toISOString();
try {
const toTime = new Intl.DateTimeFormat("en-US", {
timeZone: toTZ,
dateStyle: "full",
timeStyle: "long",
}).format(new Date(utc));
document.getElementById("convertResult").textContent = `Converted time in ${toTZ}: ${toTime}`;
} catch (e) {
document.getElementById("convertResult").textContent = `Error converting time.`;
}
}
let countdownState = 'stopped';
let countdownTarget = null;
let countdownInterval = null;
function toggleCountdown() {
const btn = document.getElementById("countdownControl");
const resetBtn = document.getElementById("resetCountdown");
if (countdownState === 'stopped') {
countdownTarget = new Date(document.getElementById("countdownDate").value).getTime();
if (!countdownTarget) return;
countdownState = 'running';
btn.textContent = "Stop";
resetBtn.style.display = 'none';
countdownInterval = setInterval(updateCountdown, 1000);
} else if (countdownState === 'running') {
countdownState = 'paused';
clearInterval(countdownInterval);
btn.textContent = "Resume";
resetBtn.style.display = 'inline-block';
} else if (countdownState === 'paused') {
countdownState = 'running';
btn.textContent = "Stop";
resetBtn.style.display = 'none';
countdownInterval = setInterval(updateCountdown, 1000);
}
}
function updateCountdown() {
const output = document.getElementById("countdownResult");
const now = new Date().getTime();
const distance = countdownTarget - now;
if (distance < 0) {
clearInterval(countdownInterval);
countdownState = 'stopped';
document.getElementById("countdownControl").textContent = "Start Countdown";
document.getElementById("resetCountdown").style.display = 'inline-block';
output.textContent = "Countdown finished!";
return;
}
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
output.textContent = `Time left: ${days}d ${hours}h ${minutes}m ${seconds}s`;
}
function resetCountdown() {
clearInterval(countdownInterval);
countdownState = 'stopped';
document.getElementById("countdownControl").textContent = "Start Countdown";
document.getElementById("resetCountdown").style.display = 'none';
document.getElementById("countdownResult").textContent = "";
}
function calculateAge() {
const birth = new Date(document.getElementById("birthDate").value);
const now = new Date();
let years = now.getFullYear() - birth.getFullYear();
let months = now.getMonth() - birth.getMonth();
let days = now.getDate() - birth.getDate();
if (days < 0) {
months--;
const previousMonth = new Date(now.getFullYear(), now.getMonth(), 0);
days += previousMonth.getDate();
}
if (months < 0) {
years--;
months += 12;
}
document.getElementById("ageResult").textContent =
`You are ${years} years, ${months} months, and ${days} days old.`;
}
function convertTimestamp() {
const ts = parseInt(document.getElementById("unixTimestamp").value);
if (isNaN(ts)) return;
const date = new Date(ts * 1000);
document.getElementById("timestampResult").textContent = `Date: ${date.toLocaleString()}`;
}
// New functionality: Show tab based on URL hash
function showToolByHash() {
const hash = window.location.hash.replace('#', '').toLowerCase();
const tabs = document.querySelectorAll('.tab');
const tools = document.querySelectorAll('.tool');
const index = tabNames.indexOf(hash);
if (index >= 0) {
tabs.forEach((tab, i) => tab.classList.toggle('active', i === index));
tools.forEach((tool, i) => tool.classList.toggle('active', i === index));
}
}
window.addEventListener('DOMContentLoaded', showToolByHash);
window.addEventListener('hashchange', showToolByHash);