-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual.js
90 lines (69 loc) · 3.55 KB
/
manual.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
function getCurrentDate() {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth(); // Month is zero-indexed, so no adjustment is needed
const day = now.getDate();
return new Date(year, month, day);
}
function calculateSunset(latitude, longitude, date) {
// Convert latitude and longitude to radians
const phi = latitude * Math.PI / 180;
const lambda_ = longitude * Math.PI / 180;
// Calculate the Julian Day (JD)
const startOfYear = new Date(date.getFullYear(), 0, 1);
const n = (date - startOfYear) / (1000 * 60 * 60 * 24);
const jd = n + 1.0008;
// Calculate the solar noon (in days since 2000-01-01 12:00 UTC)
const J = jd - longitude / 360;
// Calculate the solar mean anomaly (M)
let M = (357.5291 + 0.98560028 * J) % 360;
const M_rad = M * Math.PI / 180;
// Calculate the equation of center (C)
const C = 1.9148 * Math.sin(M_rad) + 0.0200 * Math.sin(2 * M_rad) + 0.0003 * Math.sin(3 * M_rad);
// Calculate the ecliptic longitude (lambda_sun)
let lambda_sun = (M + C + 180 + 102.9372) % 360;
const lambda_sun_rad = lambda_sun * Math.PI / 180;
// Calculate the Sun's declination (delta)
const delta = Math.asin(Math.sin(lambda_sun_rad) * Math.sin(23.44 * Math.PI / 180));
// Calculate the hour angle (H)
const zenith = 90.83 * Math.PI / 180; // Official sunset zenith
const cos_H = (Math.cos(zenith) - Math.sin(phi) * Math.sin(delta)) / (Math.cos(phi) * Math.cos(delta));
const H = Math.acos(Math.min(Math.max(cos_H, -1), 1)); // Ensure cos_H is within valid range
// Convert hour angle to hours
const H_deg = H * 180 / Math.PI;
const H_hours = H_deg / 15;
// Calculate solar noon and sunset time
const solar_noon = 2451545.0009 + J + 0.0053 * Math.sin(M_rad) - 0.0069 * Math.sin(2 * lambda_sun_rad);
const sunset_jd = solar_noon + H_hours / 24;
// Convert Julian Day to datetime
const sunset_utc = new Date(Date.UTC(2000, 0, 1, 12) + (sunset_jd - 2451545.0) * 86400000);
return sunset_utc;
}
// Function to calculate and display sunset time
function calculateAndDisplaySunset() {
// Get user-provided date
const day = parseInt(document.getElementById("day").value);
const month = parseInt(document.getElementById("month").value) - 1; // Month is zero-indexed
const year = parseInt(document.getElementById("year").value);
const date = new Date(year, month, day);
// Get user-provided latitude and longitude
const latitude = parseFloat(document.getElementById("latitude").value);
const longitude = parseFloat(document.getElementById("longitude").value);
// Calculate sunset in UTC
const sunset_utc = calculateSunset(latitude, longitude, date);
// Adjust for local time (Bangladesh Standard Time, UTC+6)
const timezoneOffset = 6;
const sunset_local = new Date(sunset_utc.getTime() + timezoneOffset * 60 * 60 * 1000);
// Format time with seconds
function formatTime(date) {
return date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true });
}
// Display results
document.getElementById("date").textContent = date.toDateString();
document.getElementById("sunsetUTC").textContent = formatTime(sunset_utc);
document.getElementById("sunsetLocal").textContent = formatTime(sunset_local);
}
// Attach event listener to the button
document.getElementById("calculateButton").addEventListener("click", calculateAndDisplaySunset);
// Calculate sunset for the current date on page load
calculateAndDisplaySunset();