-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (83 loc) · 3.88 KB
/
Copy pathindex.js
File metadata and controls
95 lines (83 loc) · 3.88 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
// Add event listener to the button
const mainContent = document.getElementById("main-content");
const dateForm = document.getElementById("date-form");
const resultSection = document.getElementById("result-section");
const inputDate = document.getElementById("special-date");
let relashionshipInitialDate = -1;
let translations = {};
// add scroll smooth behavior to the body
document.body.style.scrollBehavior = "smooth";
document.addEventListener("DOMContentLoaded", async function () {
// User agent sniffing for basic mobile detection
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (isMobile) {
// Setup for mobile (iOS/Android) - use native date picker
inputDate.setAttribute("type", "date"); // Change input type to date
inputDate.addEventListener("change", function (event) {
if (event.target.value) {
// The value from <input type="date"> is in 'YYYY-MM-DD' format.
// Parse as local date to avoid timezone issues.
const [year, month, day] = event.target.value.split('-').map(Number);
relashionshipInitialDate = new Date(year, month - 1, day); // month is 0-indexed
} else {
relashionshipInitialDate = -1;
}
});
inputDate.addEventListener("focus", function () {
resultSection.classList.add("hidden"); // Hide the result section
resultSection.classList.remove("fade-in"); // Ensure fade-in is removed
});
} else {
// Setup for desktop - use flatpickr
flatpickr("#special-date", {
maxDate: "today",
dateFormat: "Y-m-d", // Adjust the date format as needed
onValueUpdate: function (selectedDates, dateStr, instance) {
// Handle the date selection
relashionshipInitialDate = selectedDates[0] ? selectedDates[0] : -1;
},
onOpen: function (selectedDates, dateStr, instance) {
resultSection.classList.add("hidden"); // Hide the result section when the date picker opens
resultSection.classList.remove("fade-in"); // Ensure fade-in is removed
}
});
}
});
dateForm.addEventListener("submit", (event) => {
event.preventDefault(); // Prevent the default form submission
if (relashionshipInitialDate === -1) {
alert("Please select a valid date.");
return;
}
days = calculateDaysBetweenDates(new Date(relashionshipInitialDate), new Date())
// calculate the number of years
const years = Math.floor(days / 365);
// calculate the number of months
const months = Math.floor((days % 365) / 30);
// calculate the number of days
const remainingDays = days % 30;
// Display the result
const result = document.getElementById("love-duration");
if (years > 0) {
result.innerHTML = `<p>${years} ${translations.years}</p><p>${months} ${translations.months}</p><p>${remainingDays} ${translations.days}</p>`;
} else if (months > 0) {
result.innerHTML = `<p>${months} ${translations.months}</p><p>${remainingDays} ${translations.days}</p>`;
} else {
result.innerHTML = `<p>${remainingDays} ${translations.days}</p>`;
}
document.getElementById("special-date").value = "";
relashionshipInitialDate = -1; // Reset the date variable
// Remove the "hidden" class from the result section
resultSection.classList.remove("hidden");
resultSection.classList.add("fade-in");
});
/**
* Calculate the number of days between two dates.
* @param {Date} date1 - The first date.
* @param {Date} date2 - The second date.
*/
function calculateDaysBetweenDates(date1, date2) {
const timeDifference = Math.abs(date2 - date1);
const daysDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
return daysDifference;
}