Just gonna go back to basics lol
This commit is contained in:
parent
7ce674243a
commit
8950950789
29
index.js
29
index.js
|
@ -9,22 +9,23 @@ app.get('/', (req, res) => {
|
|||
|
||||
app.get('/time.php', (req, res) => {
|
||||
const tz = req.query.zone || 'UTC';
|
||||
const date = new Date();
|
||||
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) {
|
||||
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) {
|
||||
res.status(400).send('Invalid timezone');
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue