TypeScript の Tips
日付処理#
日付フォーマット#
- デフォルトでは変換関数みたいなのがないっぽいので、自前で用意する必要があるっぽい。
- 以下のようなユーティリティ関数を用意しておくと良い
1function formatDate(date: Date, format: string): string {
2 if (!format) format = "YYYY-MM-DD hh:mm:ss.SSS"
3
4 format = format.replace(/YYYY/g, date.getFullYear().toString())
5 format = format.replace(/MM/g, ("0" + (date.getMonth() + 1)).slice(-2))
6 format = format.replace(/DD/g, ("0" + date.getDate()).slice(-2))
7 format = format.replace(/hh/g, ("0" + date.getHours()).slice(-2))
8 format = format.replace(/mm/g, ("0" + date.getMinutes()).slice(-2))
9 format = format.replace(/ss/g, ("0" + date.getSeconds()).slice(-2))
10 if (format.match(/S/g)) {
11 var milliSeconds = ("00" + date.getMilliseconds()).slice(-3)
12 var length = format.match(/S/g).length
13 for (var i = 0; i < length; i++)
14 format = format.replace(/S/, milliSeconds.substring(i, i + 1))
15 }
16 return format
17}文字列から Date への変換#
Date.parse()でミリ秒に変換したものをnew Date()に入れれば良い。Date.parse()はある程度無茶振りが利くっぽい。
1let time = Date.parse("2001");
2let date = new Date(time);
3console.log(date.toISOString()); // 出力:2001-01-01T00:00:00.000Z
4
5time = Date.parse("2001-02");
6date = new Date(time);
7console.log(date.toISOString()); // 出力:2001-02-01T00:00:00.000Z
8
9time = Date.parse("2001-02-03");
10date = new Date(time);
11console.log(date.toISOString()); // 出力:2001-02-03T00:00:00.000Z
12
13time = Date.parse("2001-02-03T04:05");
14date = new Date(time);
15console.log(date.toISOString()); // 出力:2001-02-02T19:05:00.000Z
16time = Date.parse("2001-02-03T04:05+09:00");
17date = new Date(time);
18console.log(date.toISOString()); // 出力:2001-02-02T19:05:00.000Z
19time = Date.parse("2001-02-03T04:05Z");
20date = new Date(time);
21console.log(date.toISOString()); // 出力:2001-02-03T04:05:00.000Z
22
23time = Date.parse("2001-02-03T04:05:06");
24date = new Date(time);
25console.log(date.toISOString()); // 出力:2001-02-02T19:05:06.000Z
26time = Date.parse("2001-02-03T04:05:06+09:00");
27date = new Date(time);
28console.log(date.toISOString()); // 出力:2001-02-02T19:05:06.000Z
29time = Date.parse("2001-02-03T04:05:06Z");
30date = new Date(time);
31console.log(date.toISOString()); // 出力:2001-02-03T04:05:06.000Z
32
33time = Date.parse("2001-02-03T04:05:06.078");
34date = new Date(time);
35console.log(date.toISOString()); // 出力:2001-02-02T19:05:06.078Z
36time = Date.parse("2001-02-03T04:05:06.078+09:00");
37date = new Date(time);
38console.log(date.toISOString()); // 出力:2001-02-02T19:05:06.078Z
39time = Date.parse("2001-02-03T04:05:06.078Z");
40date = new Date(time);
41console.log(date.toISOString()); // 出力:2001-02-03T04:05:06.078Z
42
43time = Date.parse("+123456-07-08T09:10:11.012");
44date = new Date(time);
45console.log(date.toISOString()); // 出力:+123456-07-08T00:10:11.012Z
46time = Date.parse("+123456-07-08T09:10:11.012+09:00");
47date = new Date(time);
48console.log(date.toISOString()); // 出力:+123456-07-08T00:10:11.012Z
49time = Date.parse("+123456-07-08T09:10:11.012Z");
50date = new Date(time);
51console.log(date.toISOString()); // 出力:+123456-07-08T09:10:11.012Z
52
53time = Date.parse("");
54console.log(time); // 出力:NaN
ここに掲載する記事はまだありません。
