teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

ソース修正

2021/09/09 00:39

投稿

退会済みユーザー
answer CHANGED
@@ -9,7 +9,7 @@
9
9
  ➡ [サンプル](https://codepen.io/i8086x/pen/LYLxzLW?editors=0012)
10
10
 
11
11
 
12
- **補足:**
12
+ **補足(Dateをイテラブルにしてまう):**
13
13
 
14
14
  自分で書く案、一個挙げときます。もし頻繁にDateオブジェクトからその配列にする場合があるんやったら、いっそDateをイテラブルに拡張してしまって、スプレッド構文でバラせるようにしとけば?
15
15
 
@@ -39,4 +39,21 @@
39
39
  console.log(x); // => 2021,8,8,19,12,34,567 が順に出力される
40
40
  }
41
41
  ```
42
- みたいな。 ➡ [サンプル](https://codepen.io/i8086x/pen/ExXZbEE?editors=0012)
42
+ みたいな。 ➡ [サンプル](https://codepen.io/i8086x/pen/ExXZbEE?editors=0012)
43
+
44
+ **補足(Intl.DateTimeFormat使うてみる):**
45
+
46
+ ```javascript
47
+ const date = new Date('2021-09-08T19:12:34.567');
48
+
49
+ const arr = new Intl.DateTimeFormat('ja-jp', {
50
+ year: 'numeric', month: 'numeric', day: 'numeric',
51
+ hour: 'numeric', minute: 'numeric', second: 'numeric', fractionalSecondDigits: 3
52
+ })
53
+ .formatToParts(date)
54
+ .filter(({type}) => type !== 'literal')
55
+ .map(({value}, i) => +value - (i==1));
56
+
57
+ console.log(arr); // => [2021,8,8,19,12,34,567]
58
+ ```
59
+ ➡ [サンプル](https://codepen.io/i8086x/pen/rNwjrVj?editors=0012)

2

ソース修正

2021/09/09 00:39

投稿

退会済みユーザー
answer CHANGED
@@ -35,7 +35,7 @@
35
35
 
36
36
  console.log(arr); // => [2021,8,8,19,12,34,567]
37
37
 
38
- for (x of d) {
38
+ for (let x of d) {
39
39
  console.log(x); // => 2021,8,8,19,12,34,567 が順に出力される
40
40
  }
41
41
  ```

1

ソース修正

2021/09/08 11:25

投稿

退会済みユーザー
answer CHANGED
@@ -6,4 +6,37 @@
6
6
 
7
7
  console.log(arr); // => [2021,8,8,18,40,33,301]
8
8
  ```
9
- ➡ [サンプル](https://codepen.io/i8086x/pen/LYLxzLW?editors=0012)
9
+ ➡ [サンプル](https://codepen.io/i8086x/pen/LYLxzLW?editors=0012)
10
+
11
+
12
+ **補足:**
13
+
14
+ 自分で書く案、一個挙げときます。もし頻繁にDateオブジェクトからその配列にする場合があるんやったら、いっそDateをイテラブルに拡張してしまって、スプレッド構文でバラせるようにしとけば?
15
+
16
+ ゆうて、こない
17
+ ```javascript
18
+ Date.prototype[Symbol.iterator] = function* () {
19
+ yield* [
20
+ this.getFullYear(),
21
+ this.getMonth(),
22
+ this.getDate(),
23
+ this.getHours(),
24
+ this.getMinutes(),
25
+ this.getSeconds(),
26
+ this.getMilliseconds()
27
+ ];
28
+ }
29
+ ```
30
+ な仕込みしといてからの
31
+ ```javascript
32
+ const d = new Date('2021-09-08T19:12:34.567');
33
+
34
+ const arr = [...d];
35
+
36
+ console.log(arr); // => [2021,8,8,19,12,34,567]
37
+
38
+ for (x of d) {
39
+ console.log(x); // => 2021,8,8,19,12,34,567 が順に出力される
40
+ }
41
+ ```
42
+ みたいな。 ➡ [サンプル](https://codepen.io/i8086x/pen/ExXZbEE?editors=0012)