36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
const express = require('express');
|
|
|
|
const app = express();
|
|
const port = process.env.SERVER_PORT || 3000;
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello, world!');
|
|
});
|
|
|
|
app.get('/time.php', (req, res) => {
|
|
const tz = req.query.zone || 'UTC';
|
|
try {
|
|
const date = new Date();
|
|
const options = { timeZone: tz, hour12: false };
|
|
|
|
// No leading zeros here
|
|
const year = date.toLocaleString('en-GB', { ...options, year: 'numeric' });
|
|
const month = date.toLocaleString('en-GB', { ...options, month: 'numeric' });
|
|
const day = date.toLocaleString('en-GB', { ...options, day: 'numeric' });
|
|
const hour = date.toLocaleString('en-GB', { ...options, hour: 'numeric', hourCycle: 'h23' });
|
|
const minute = date.toLocaleString('en-GB', { ...options, minute: 'numeric' });
|
|
const second = date.toLocaleString('en-GB', { ...options, second: 'numeric' });
|
|
|
|
// Concatenate directly: DDMMYYYYHHMMSS
|
|
const timeString = `${day}${month}${year}${hour}${minute}${second}`;
|
|
res.send(timeString);
|
|
} catch (err) {
|
|
res.status(400).send('Invalid timezone');
|
|
}
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on port ${port}`);
|
|
}); |