Compare commits
2 commits
7ce674243a
...
925fbe76a5
Author | SHA1 | Date | |
---|---|---|---|
|
925fbe76a5 | ||
|
8950950789 |
32
index.js
32
index.js
|
@ -8,23 +8,27 @@ app.get('/', (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/time.php', (req, res) => {
|
app.get('/time.php', (req, res) => {
|
||||||
|
// Logging
|
||||||
|
console.log(`time.php called; Headers: ${JSON.stringify(req.headers)}`);
|
||||||
|
|
||||||
const tz = req.query.zone || 'UTC';
|
const tz = req.query.zone || 'UTC';
|
||||||
|
const date = new Date();
|
||||||
try {
|
try {
|
||||||
const date = new Date();
|
|
||||||
const options = { timeZone: tz, hour12: false };
|
const options = { timeZone: tz, hour12: false };
|
||||||
|
const formatter = new Intl.DateTimeFormat('en-GB', {
|
||||||
// No leading zeros here
|
...options,
|
||||||
const year = date.toLocaleString('en-GB', { ...options, year: 'numeric' });
|
year: 'numeric',
|
||||||
const month = date.toLocaleString('en-GB', { ...options, month: 'numeric' });
|
month: '2-digit',
|
||||||
const day = date.toLocaleString('en-GB', { ...options, day: 'numeric' });
|
day: '2-digit',
|
||||||
const hour = date.toLocaleString('en-GB', { ...options, hour: 'numeric', hourCycle: 'h23' });
|
hour: '2-digit',
|
||||||
const minute = date.toLocaleString('en-GB', { ...options, minute: 'numeric' });
|
minute: '2-digit',
|
||||||
const second = date.toLocaleString('en-GB', { ...options, second: 'numeric' });
|
second: '2-digit'
|
||||||
|
});
|
||||||
// Concatenate directly: DDMMYYYYHHMMSS
|
const parts = formatter.formatToParts(date);
|
||||||
const timeString = `${day}${month}${year}${hour}${minute}${second}`;
|
const get = type => parts.find(p => p.type === type).value;
|
||||||
res.send(timeString);
|
const formatted = `${get('day')}${get('month')}${get('year')}${get('hour')}${get('minute')}${get('second')}`;
|
||||||
} catch (err) {
|
res.send(formatted);
|
||||||
|
} catch (e) {
|
||||||
res.status(400).send('Invalid timezone');
|
res.status(400).send('Invalid timezone');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue