-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.js
More file actions
273 lines (226 loc) · 10.8 KB
/
date.js
File metadata and controls
273 lines (226 loc) · 10.8 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
document.addEventListener('DOMContentLoaded', () => {
const startYearSelect = document.getElementById('start-year');
const startMonthSelect = document.getElementById('start-month');
const startDaySelect = document.getElementById('start-day');
const endYearSelect = document.getElementById('end-year');
const endMonthSelect = document.getElementById('end-month');
const endDaySelect = document.getElementById('end-day');
const startYear = 1700; // Adjust this value as needed
const currentYear = 3000; // Adjust this value as needed
// Populate year dropdowns
for (let year = startYear; year <= currentYear; year++) {
let option = document.createElement('option');
option.value = year;
option.text = year;
startYearSelect.add(option.cloneNode(true));
endYearSelect.add(option.cloneNode(true));
}
// Populate month dropdowns
const monthNames = ['Jan (1)', 'Feb (2)', 'Mar (3)', 'Apr (4)', 'May (5)', 'Jun (6)', 'Jul (7)', 'Aug (8)', 'Sep (9)', 'Oct (10)', 'Nov (11)', 'Dec (12)'];
monthNames.forEach((month, index) => {
let option = document.createElement('option');
option.value = index + 1; // Month index starts at 0, so add 1 for correct month value
option.text = month;
startMonthSelect.add(option.cloneNode(true));
endMonthSelect.add(option.cloneNode(true));
});
// Function to populate days based on selected year and month
function populateDays(selectDay, year, month) {
if (!year || !month) return;
const daysInMonth = new Date(year, month, 0).getDate();
selectDay.innerHTML = ''; // Clear previous options
for (let day = 1; day <= daysInMonth; day++) {
let option = document.createElement('option');
option.value = day;
option.text = day;
selectDay.add(option);
}
}
// Populate days on year/month change
function updateDays() {
const startYear = parseInt(startYearSelect.value, 10);
const startMonth = parseInt(startMonthSelect.value, 10);
// Store the current selected day for both start and end dates
const selectedStartDay = parseInt(startDaySelect.value, 10);
const selectedEndDay = parseInt(endDaySelect.value, 10);
// Repopulate the start day dropdown
populateDays(startDaySelect, startYear, startMonth);
// Reselect the previous start day if possible, otherwise select the last day of the month
if (selectedStartDay <= startDaySelect.options.length) {
startDaySelect.value = selectedStartDay;
} else {
startDaySelect.value = startDaySelect.options.length;
}
const endYear = parseInt(endYearSelect.value, 10);
const endMonth = parseInt(endMonthSelect.value, 10);
// Repopulate the end day dropdown
populateDays(endDaySelect, endYear, endMonth);
// Reselect the previous end day if possible, otherwise select the last day of the month
if (selectedEndDay <= endDaySelect.options.length) {
endDaySelect.value = selectedEndDay;
} else {
endDaySelect.value = endDaySelect.options.length;
}
}
startYearSelect.addEventListener('change', updateDays);
startMonthSelect.addEventListener('change', updateDays);
endYearSelect.addEventListener('change', updateDays);
endMonthSelect.addEventListener('change', updateDays);
// Function to calculate and display elapsed time
window.calculateElapse = function() {
const startYear = parseInt(startYearSelect.value, 10);
const startMonth = parseInt(startMonthSelect.value, 10);
const startDay = parseInt(startDaySelect.value, 10);
const endYear = parseInt(endYearSelect.value, 10);
const endMonth = parseInt(endMonthSelect.value, 10);
const endDay = parseInt(endDaySelect.value, 10);
// Validate selected dates
if (!startYear || !startMonth || !startDay || !endYear || !endMonth || !endDay ||
new Date(endYear, endMonth - 1, endDay) < new Date(startYear, startMonth - 1, startDay)) {
document.querySelector('.date_span').innerHTML = '<span style="color: red;">End date must be greater than start date.</span>';
return;
}
const startDate = new Date(startYear, startMonth - 1, startDay);
const endDate = new Date(endYear, endMonth - 1, endDay);
// Calculate total days
let totalDays = Math.floor((endDate - startDate) / (1000 * 60 * 60 * 24));
// Calculate elapsed years, months, and days
let elapsedYears = endDate.getFullYear() - startDate.getFullYear();
let elapsedMonths = endDate.getMonth() - startDate.getMonth();
let elapsedDays = endDate.getDate() - startDate.getDate();
if (elapsedDays < 0) {
let daysInLastMonth = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
elapsedDays += daysInLastMonth;
elapsedMonths--;
}
if (elapsedMonths < 0) {
elapsedMonths += 12;
elapsedYears--;
}
// Calculate total weeks and remaining days
let totalWeeks = Math.floor(totalDays / 7);
let remainingDays = totalDays % 7;
// Calculate total months
let totalMonths = elapsedYears * 12 + elapsedMonths;
// Output
document.querySelector('.date_span').innerHTML = `${elapsedYears} years, ${elapsedMonths} months, and ${elapsedDays} days<br><br>Or, ${totalMonths} months and ${elapsedDays} days<br><br>Or, ${totalWeeks} weeks and ${remainingDays} days<br><br>Or, ${totalDays} days`;
}
// Set both start and end dates to today's date
function setTodayDate() {
const today = new Date();
// Set start date to today's date
startYearSelect.value = today.getFullYear();
startMonthSelect.value = today.getMonth() + 1; // Months are 0-indexed
populateDays(startDaySelect, today.getFullYear(), today.getMonth() + 1);
startDaySelect.value = today.getDate();
// Set end date to today's date
endYearSelect.value = today.getFullYear();
endMonthSelect.value = today.getMonth() + 1; // Months are 0-indexed
populateDays(endDaySelect, today.getFullYear(), today.getMonth() + 1);
endDaySelect.value = today.getDate();
updateDays(); // Make sure to call this after setting today's date to update days correctly
}
// Reset button functionality
document.getElementById('reset-button').addEventListener('click', function() {
setTodayDate(); // Set the date pickers to today's date
document.querySelector('.date_span').innerHTML = ''; // Clear the result display
});
setTodayDate(); // Initialize both dates to today's date
});
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/date/service-worker.js')
.then((registration) => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch((error) => {
console.error('Service Worker registration failed:', error);
});
});
}
const themeToggle = document.getElementById('themeToggle');
// Function to set the theme based on user preference or system setting
function setTheme(theme) {
document.body.className = theme;
themeToggle.textContent = theme === 'dark' ? '🌙' : '🔆';
localStorage.setItem('theme', theme);
}
// Check local storage for user preference
const storedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (storedTheme) {
setTheme(storedTheme);
} else if (systemPrefersDark) {
setTheme('dark');
} else {
setTheme('light');
}
// Event listener for button click to toggle theme
themeToggle.addEventListener('click', () => {
const currentTheme = document.body.classList.contains('dark') ? 'dark' : 'light';
setTheme(currentTheme === 'dark' ? 'light' : 'dark');
});
document.addEventListener('DOMContentLoaded', () => {
const footer = document.querySelector('.footer');
const btnInstall = document.getElementById('btnInstall');
let deferredInstallPrompt;
// Check if the app is running as a PWA (Standalone mode)
const isPWA = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone;
// If the app is already installed as a PWA, update the footer content
if (isPWA) {
if (footer) {
footer.innerHTML = `
<p> Copyright © 2024-2025 Date Mate</p>
`;
}
// Hide the install button as the app is installed
if (btnInstall) {
btnInstall.style.display = 'none';
}
} else {
// Show install button on web version if app is not installed
if (btnInstall) {
btnInstall.style.display = 'block';
btnInstall.textContent = 'Install App';
}
}
// Listen for 'beforeinstallprompt' to save the install prompt
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault(); // Prevent the default install prompt from showing automatically
deferredInstallPrompt = e; // Save the install prompt
console.log('Install prompt saved');
// Show the install button when the prompt is available
if (btnInstall) {
btnInstall.style.display = 'block';
btnInstall.textContent = 'Install App';
}
});
// Listen for install button click
btnInstall.addEventListener('click', () => {
if (deferredInstallPrompt) {
deferredInstallPrompt.prompt(); // Show the install prompt
deferredInstallPrompt.userChoice.then((choice) => {
if (choice.outcome === 'accepted') {
console.log('User accepted the installation');
btnInstall.style.display = 'none'; // Hide the install button after installation
} else {
console.log('User dismissed the installation');
}
});
} else {
// If the app is already installed or there's no install prompt available
alert('The app is already installed on your device.');
}
});
// Listen for the 'appinstalled' event and update the footer
window.addEventListener('appinstalled', () => {
console.log('App installed, hiding the button');
btnInstall.style.display = 'none'; // Hide the install button after the app is installed
// Update the footer when the app is installed as a PWA
if (footer) {
footer.innerHTML = `
<p> Copyright © 2024-2025 Date Mate</p>
`;
}
});
});