1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export async function countdown(req: express.Request, res: express.Response) {
// Target Time
// const target_time = req.params.time ?? ;
const target_time = String(req.query.time) ?? "2023-09-02 17:00:00";
// Minutes that have passed post target time
const post_expire_duration = (parseInt(req.params.duration) ?? 25) * 60;
const target_d = new Date(target_time).getTime();
const now = new Date().getTime();
let remaining_seconds = Math.round(target_d / 1000) -
Math.round(now / 1000);
if (remaining_seconds < 0)
remaining_seconds = 0;
let response = {
"s": remaining_seconds,
"t": "before",
"time": target_time,
};
if (remaining_seconds <= 0 && remaining_seconds >= post_expire_duration)
response = { ...response, "t": "during" };
if (remaining_seconds <= 0 && remaining_seconds < post_expire_duration)
response = { ...response, "t": "after" };
return res.json(response);
}