Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

NAME

eclipsetoday - Determine if a solar or lunar eclipse is happening right now

DESCRIPTION

The script checks if there's a solar or lunar eclipse 
currently occurring anywhere on Earth. The script 
utilizes a hardcoded list of known eclipses for the 
years 2024-2026.

It will return a JSON response indicating whether an eclipse is happening 
at the time of the request, along with details about the eclipse if applicable.

USAGE

The script is designed to be run in a server environment, responding to 
incoming HTTP requests. Upon receiving a request, it will return JSON data 
in the following format:

```
{
  "isEclipseHappening": true/false,
  "eclipseDetails": {
    "date": "2024-04-08T00:00:00Z",
    "kind": "solar",
    "type": "total",
    "place": "partial: North and Central America; total: northern Mexico, central U.S., and eastern Canada"
  },
  "checkedAt": "2024-04-08T00:00:00Z"
}
```

If an error occurs during processing, the script will return a JSON error 
message with details about the failure.
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
/**
* This app will check if there's a solar or lunar eclipse happening anywhere on Earth right now.
* We'll use a hardcoded list of known eclipses for the upcoming years.
* The app will return JSON specifying whether there's an eclipse happening.
*/
export default async function server(request: Request): Promise<Response> {
try {
// Hardcoded list of eclipses for 2024-2026
const eclipses = [
{ date: '2024-03-25T00:00:00Z', kind: 'lunar', type: 'penumbral', place: 'North and South America' },
{ date: '2024-04-08T00:00:00Z', kind: 'solar', type: 'total', place: 'partial: North and Central America; total: northern Mexico, central U.S., and eastern Canada' },
{ date: '2024-09-18T00:00:00Z', kind: 'lunar', type: 'partial', place: 'North and South America, Europe, and Africa' },
{ date: '2024-10-02T00:00:00Z', kind: 'solar', type: 'annular', place: 'partial: South Pacific and South America; annular: southern Chile and Argentina' },
{ date: '2025-03-14T00:00:00Z', kind: 'lunar', type: 'total', place: 'North and South America, Europe, and Africa' },
{ date: '2025-03-29T00:00:00Z', kind: 'solar', type: 'partial', place: 'northwestern Africa, Europe, and northwestern Russia' },
{ date: '2025-09-07T00:00:00Z', kind: 'lunar', type: 'total', place: 'Europe, Africa, Asia, and Australia' },
{ date: '2025-09-21T00:00:00Z', kind: 'solar', type: 'partial', place: 'New Zealand and Antarctica' },
{ date: '2026-02-17T00:00:00Z', kind: 'solar', type: 'annular', place: 'partial: southern Chile and Argentina, Antarctica, and southern Africa; annular: Antarctica' },
{ date: '2026-03-03T00:00:00Z', kind: 'lunar', type: 'total', place: 'Asia, Australia, and North and South America' },
{ date: '2026-08-12T00:00:00Z', kind: 'solar', type: 'total', place: 'partial: Alaska, Canada, and Greenland; total: eastern Greenland' },
{ date: '2026-08-28T00:00:00Z', kind: 'lunar', type: 'partial', place: 'North and South America, Europe, and Africa' }
];
// Check if there's an ongoing eclipse
const now = new Date();
const ongoingEclipse = eclipses.find((eclipse) => {
const eclipseDate = new Date(eclipse.date);
// Assume an eclipse lasts for 6 hours (21600000 milliseconds)
const eclipseEndDate = new Date(eclipseDate.getTime() + 21600000);
return now >= eclipseDate && now <= eclipseEndDate;
});
// Prepare the response
const result = {
isEclipseHappening: !!ongoingEclipse,
eclipseDetails: ongoingEclipse || null,
checkedAt: now.toISOString(),
};
// Return the result as JSON
return new Response(JSON.stringify(result, null, 2), {
headers: {
'Content-Type': 'application/json',
},
});
} catch (error) {
console.error('Error:', error);
// Return an error response
return new Response(JSON.stringify({
error: 'An error occurred while checking for eclipses',
details: error.message
}, null, 2), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
}
ejfox-eclipsetoday.web.val.run
August 23, 2024