Compare commits
No commits in common. "925fbe76a5b7362f45d02b8c67ba59441e44c9bf" and "7ce674243a3b709ad3ae5a4635dd24560816530d" have entirely different histories.
925fbe76a5
...
7ce674243a
32
index.js
32
index.js
|
@ -8,27 +8,23 @@ app.get('/', (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 date = new Date();
|
||||
try {
|
||||
const date = new Date();
|
||||
const options = { timeZone: tz, hour12: false };
|
||||
const formatter = new Intl.DateTimeFormat('en-GB', {
|
||||
...options,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
});
|
||||
const parts = formatter.formatToParts(date);
|
||||
const get = type => parts.find(p => p.type === type).value;
|
||||
const formatted = `${get('day')}${get('month')}${get('year')}${get('hour')}${get('minute')}${get('second')}`;
|
||||
res.send(formatted);
|
||||
} catch (e) {
|
||||
|
||||
// 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');
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue