Skip to content

Commit 9b9a860

Browse files
committed
Add examples
1 parent e752e9f commit 9b9a860

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

JavaScript/1-datetime.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// Requires node.js 18
4+
5+
const DATE_TIME_FORMAT = {
6+
year: 'numeric',
7+
month: '2-digit',
8+
day: '2-digit',
9+
hour: '2-digit',
10+
minute: '2-digit',
11+
second: '2-digit',
12+
timeZoneName: 'longOffset',
13+
};
14+
15+
// Sweden
16+
const formatter = new Intl.DateTimeFormat('sv-SE', DATE_TIME_FORMAT);
17+
18+
const date = new Date();
19+
const parts = formatter.formatToParts(date);
20+
console.log(parts);
21+
22+
const s = parts.map(({ value }) => value).join('');
23+
console.log(s);
24+
// Output: 2025-06-27 02:00:54 GMT+03:00

JavaScript/2-iso.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// Requires node.js 18
4+
5+
const DATE_TIME_FORMAT = {
6+
year: 'numeric',
7+
month: '2-digit',
8+
day: '2-digit',
9+
hour: '2-digit',
10+
minute: '2-digit',
11+
second: '2-digit',
12+
timeZoneName: 'longOffset',
13+
};
14+
15+
const formatter = new Intl.DateTimeFormat('en-GB', DATE_TIME_FORMAT);
16+
17+
const getLocalIsoString = () => {
18+
const now = new Date();
19+
const parts = formatter
20+
.formatToParts(now)
21+
.filter(({ type }) => type !== 'literal')
22+
.map(({ type, value }) => [type, value]);
23+
const date = Object.fromEntries(parts);
24+
const { year, month, day, hour, minute, second } = date;
25+
console.log(date);
26+
const zone = date.timeZoneName.slice(3);
27+
return `${year}-${month}-${day}T${hour}:${minute}:${second}${zone}`;
28+
};
29+
30+
// Usage
31+
32+
console.log(getLocalIsoString());
33+
// Output: 2025-06-27T02:00:54+03:00

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy