EN
TypeScript - get current time
0 points
In TypeScript Date
class is useful to get current time.
Quick solution:
xxxxxxxxxx
1
const now: number = Date.now();
2
3
console.log('Current unix millisecond time: ' + now);
xxxxxxxxxx
1
const now: Date = new Date();
2
3
console.log('Current unix millisecond time: ' + now.getTime());
4
console.log('Current date: ' + now.toDateString());
5
console.log('Current time: ' + now.toTimeString());
6
console.log('Current date & time: ' + now.toString());
Note: during using
to...String
methods it is necessaryto be careful because of specific formatting of data and time on the locale.
renderNumber
method can be replaced with built-in padStart
method.
xxxxxxxxxx
1
// ONLINE-RUNNER:browser;
2
3
const renderNumber = (value: number, length: number) => {
4
let result: string = String(value);
5
6
for (; length > result.length; length -= 1) result = '0' + result;
7
8
return result;
9
};
10
11
const renderDate = (date: Date) => {
12
const year: string = renderNumber(date.getFullYear(), 4);
13
const month: string = renderNumber(date.getMonth() + 1, 2);
14
const day: string = renderNumber(date.getDate(), 2);
15
16
return `${year}.${month}.${day}`;
17
};
18
19
const renderTime = (date: Date) => {
20
const hours: string = renderNumber(date.getHours(), 2);
21
const minutes: string = renderNumber(date.getMinutes(), 2);
22
const seconds: string = renderNumber(date.getSeconds(), 2);
23
const milliseconds: string = renderNumber(date.getMilliseconds(), 3);
24
25
return `${hours}:${minutes}:${seconds}.${milliseconds}`;
26
};
27
28
const renderDateTime = (date: Date) => {
29
const dateText: string = renderDate(date);
30
const timeText: string = renderTime(date);
31
32
return `${dateText} ${timeText}`;
33
};
34
35
// Usage example:
36
37
const now: Date = new Date();
38
39
const date: string = renderDate(now);
40
const time: string = renderTime(now);
41
const dateTime: string = renderDateTime(now);
42
43
console.log('Current date: ' + date);
44
console.log('Current time: ' + time);
45
console.log('Current date & time: ' + dateTime);
46
xxxxxxxxxx
1
// ONLINE-RUNNER:browser;
2
3
function renderNumber(value: number, length: number) {
4
let result: string = String(value);
5
for (; length > result.length; length -= 1) {
6
result = '0' + result;
7
}
8
return result;
9
}
10
11
function renderDate(date: Date) {
12
const year: number = date.getFullYear();
13
const month: number = date.getMonth() + 1;
14
const day: number = date.getDate();
15
16
const text: string =
17
renderNumber(year, 4) +
18
'.' +
19
renderNumber(month, 2) +
20
'.' +
21
renderNumber(day, 2);
22
return text;
23
}
24
25
function renderTime(date: Date) {
26
const hour: number = date.getHours();
27
const minute: number = date.getMinutes();
28
const second: number = date.getSeconds();
29
const millisecond: number = date.getMilliseconds();
30
31
const text: string =
32
renderNumber(hour, 2) +
33
':' +
34
renderNumber(minute, 2) +
35
':' +
36
renderNumber(second, 2) +
37
'.' +
38
renderNumber(millisecond, 3);
39
return text;
40
}
41
42
function renderDateTime(date: Date) {
43
const dateText: string = renderDate(date);
44
const timeText: string = renderTime(date);
45
46
return dateText + ' ' + timeText;
47
}
48
49
// Usage example:
50
51
const now: Date = new Date();
52
53
const date: string = renderDate(now);
54
const time: string = renderTime(now);
55
const dateTime: string = renderDateTime(now);
56
57
console.log('Current date: ' + date);
58
console.log('Current time: ' + time);
59
console.log('Current date & time: ' + dateTime);