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

回答編集履歴

3

コメントを受けて追記

2018/04/02 10:12

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア37463

answer CHANGED
@@ -96,4 +96,35 @@
96
96
 
97
97
  思いのほか、しんどかったなあ。
98
98
  みんな、Moment.js とか使うはずだわ……
99
- 勉強させてもらいました。
99
+ 勉強させてもらいました。
100
+
101
+ ### コメントを受けて追記
102
+ 逆引きの方が簡単なはず。コードの重複があるから適当にまとめてください。
103
+ ```js
104
+ function calcSchedule(weekNumber, year){
105
+
106
+ var getMonday = function(date) {
107
+ return new Date( date.getTime() - ( date.getDay() - 1 ) * 24 * 60 * 60 * 1000 );
108
+ }
109
+
110
+ var tempDate = new Date(year, 1, 20, 0, 0, 0);
111
+ var firstDate = getMonday( tempDate );
112
+ var monday = new Date( firstDate.getTime() + ( weekNumber - 1 ) * 7 * 24 * 60 * 60 * 1000 );
113
+ var sunday = new Date( monday.getTime() + 6 * 24 * 60 * 60 * 1000 );
114
+
115
+ return `表示週第第${weekNumber}週は
116
+ ${
117
+ monday.toLocaleDateString('ja',{year:'numeric'})
118
+ }${
119
+ monday.toLocaleDateString('ja',{month:'numeric'})
120
+ }${
121
+ monday.toLocaleDateString('ja',{day:'numeric'})
122
+ }(月)~${
123
+ sunday.toLocaleDateString('ja',{year:'numeric'})
124
+ }${
125
+ sunday.toLocaleDateString('ja',{month:'numeric'})
126
+ }${
127
+ sunday.toLocaleDateString('ja',{day:'numeric'})
128
+ }(日)です。`;
129
+ }
130
+ ```

2

訂正

2018/04/02 10:12

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア37463

answer CHANGED
@@ -31,7 +31,7 @@
31
31
  var monday = getMonday( tempDate );
32
32
  var sunday = new Date( monday.getTime() + 6 * 24 * 60 * 60 * 1000 );
33
33
 
34
- var tempDate = new Date(date);
34
+ tempDate = new Date(date);
35
35
  tempDate.setMonth(1,20);
36
36
  firstDate = getMonday( tempDate );
37
37
  if ( firstDate > date ) {

1

追記

2018/03/31 11:26

投稿

Lhankor_Mhy
Lhankor_Mhy

スコア37463

answer CHANGED
@@ -14,4 +14,86 @@
14
14
  return Math.floor( Math.round( ( time - checkDate ) / 86400000 ) / 7 ) + 1;
15
15
  }
16
16
  ```
17
- [jquery-ui/datepicker.js at 17404ced478a235651513fa7bef3473ef1b039e8 · jquery/jquery-ui · GitHub](https://github.com/jquery/jquery-ui/blob/17404ced478a235651513fa7bef3473ef1b039e8/ui/widgets/datepicker.js)
17
+ [jquery-ui/datepicker.js at 17404ced478a235651513fa7bef3473ef1b039e8 · jquery/jquery-ui · GitHub](https://github.com/jquery/jquery-ui/blob/17404ced478a235651513fa7bef3473ef1b039e8/ui/widgets/datepicker.js)
18
+
19
+ ### 追記
20
+ おそらく、「2月21日が含まれる週が第1週」という仕様だと思います。
21
+ ```js
22
+ function schedule(date){
23
+
24
+ var getMonday = function(date) {
25
+ return new Date( date.getTime() - ( date.getDay() - 1 ) * 24 * 60 * 60 * 1000 );
26
+ }
27
+
28
+ date.setHours(0,0,0);
29
+
30
+ var tempDate = new Date( date - 24 * 60 * 60 * 1000 );
31
+ var monday = getMonday( tempDate );
32
+ var sunday = new Date( monday.getTime() + 6 * 24 * 60 * 60 * 1000 );
33
+
34
+ var tempDate = new Date(date);
35
+ tempDate.setMonth(1,20);
36
+ firstDate = getMonday( tempDate );
37
+ if ( firstDate > date ) {
38
+ tempDate.setMonth(-11,20);
39
+ firstDate = getMonday( tempDate );
40
+ }
41
+
42
+ var weekNumber = Math.floor( ( date - firstDate ) / ( 24 * 60 * 60 * 1000 ) / 7 ) + 1;
43
+
44
+ return `${
45
+ date.toLocaleDateString('ja',{year:'numeric'})
46
+ }${
47
+ date.toLocaleDateString('ja',{month:'numeric'})
48
+ }${
49
+ date.toLocaleDateString('ja',{day:'numeric'})
50
+ }(${
51
+ date.toLocaleDateString('ja',{weekday:'short'})
52
+ })の週は第${weekNumber}週です。第${weekNumber}週は${
53
+ monday.toLocaleDateString('ja',{year:'numeric'})
54
+ }${
55
+ monday.toLocaleDateString('ja',{month:'numeric'})
56
+ }${
57
+ monday.toLocaleDateString('ja',{day:'numeric'})
58
+ }(月)~${
59
+ sunday.toLocaleDateString('ja',{year:'numeric'})
60
+ }${
61
+ sunday.toLocaleDateString('ja',{month:'numeric'})
62
+ }${
63
+ sunday.toLocaleDateString('ja',{day:'numeric'})
64
+ }(日)です。`;
65
+ }
66
+ ```
67
+ ```js
68
+ [
69
+ [2018,1,18],
70
+ [2018,1,19],
71
+ [2019,1,18],
72
+ [2020,1,17],
73
+ [2021,1,15],
74
+ [2022,1,21],
75
+ [2027,1,14],
76
+ [2027,1,15],
77
+ [2028,1,20],
78
+ [2028,1,21],
79
+ ].forEach(function(e){
80
+ console.log( schedule(new Date(...e)) );
81
+ });
82
+
83
+ /*
84
+ 2018年2月18日(日)の週は第52週です。第52週は2018年2月12日(月)~2018年2月18日(日)です。
85
+ 2018年2月19日(月)の週は第1週です。第1週は2018年2月19日(月)~2018年2月25日(日)です。
86
+ 2019年2月18日(月)の週は第1週です。第1週は2019年2月18日(月)~2019年2月24日(日)です。
87
+ 2020年2月17日(月)の週は第1週です。第1週は2020年2月17日(月)~2020年2月23日(日)です。
88
+ 2021年2月15日(月)の週は第1週です。第1週は2021年2月15日(月)~2021年2月21日(日)です。
89
+ 2022年2月21日(月)の週は第1週です。第1週は2022年2月21日(月)~2022年2月27日(日)です。
90
+ 2027年2月14日(日)の週は第52週です。第52週は2027年2月8日(月)~2027年2月14日(日)です。
91
+ 2027年2月15日(月)の週は第1週です。第1週は2027年2月15日(月)~2027年2月21日(日)です。
92
+ 2028年2月20日(日)の週は第53週です。第53週は2028年2月14日(月)~2028年2月20日(日)です。
93
+ 2028年2月21日(月)の週は第1週です。第1週は2028年2月21日(月)~2028年2月27日(日)です。
94
+ */
95
+ ```
96
+
97
+ 思いのほか、しんどかったなあ。
98
+ みんな、Moment.js とか使うはずだわ……
99
+ 勉強させてもらいました。