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) => {
|
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', {
|
|
||||||
...options,
|
// No leading zeros here
|
||||||
year: 'numeric',
|
const year = date.toLocaleString('en-GB', { ...options, year: 'numeric' });
|
||||||
month: '2-digit',
|
const month = date.toLocaleString('en-GB', { ...options, month: 'numeric' });
|
||||||
day: '2-digit',
|
const day = date.toLocaleString('en-GB', { ...options, day: 'numeric' });
|
||||||
hour: '2-digit',
|
const hour = date.toLocaleString('en-GB', { ...options, hour: 'numeric', hourCycle: 'h23' });
|
||||||
minute: '2-digit',
|
const minute = date.toLocaleString('en-GB', { ...options, minute: 'numeric' });
|
||||||
second: '2-digit'
|
const second = date.toLocaleString('en-GB', { ...options, second: 'numeric' });
|
||||||
});
|
|
||||||
const parts = formatter.formatToParts(date);
|
// Concatenate directly: DDMMYYYYHHMMSS
|
||||||
const get = type => parts.find(p => p.type === type).value;
|
const timeString = `${day}${month}${year}${hour}${minute}${second}`;
|
||||||
const formatted = `${get('day')}${get('month')}${get('year')}${get('hour')}${get('minute')}${get('second')}`;
|
res.send(timeString);
|
||||||
res.send(formatted);
|
} catch (err) {
|
||||||
} catch (e) {
|
|
||||||
res.status(400).send('Invalid timezone');
|
res.status(400).send('Invalid timezone');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue