質問編集履歴

1

func dateForCellAtIndexPath と func conversionDateFormat の追記

2017/09/30 17:06

投稿

swifter
swifter

スコア15

test CHANGED
File without changes
test CHANGED
@@ -63,3 +63,99 @@
63
63
 
64
64
 
65
65
  よろしくお願いいたします。
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+ ーーーーーー以下追記ーーーーーーー
78
+
79
+ ```ここに言語を入力
80
+
81
+ func dateForCellAtIndexPath(numberOfItem: Int) {
82
+
83
+ // ①「月の初日が週の何日目か」を計算する
84
+
85
+ let ordinalityOfFirstDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: firstDateOfMonth())
86
+
87
+
88
+
89
+ for i in 0 ..< numberOfItems {
90
+
91
+ // ②「月の初日」と「indexPath.item番目のセルに表示する日」の差を計算する
92
+
93
+ var dateComponents = DateComponents()
94
+
95
+
96
+
97
+ dateComponents.day = i - (ordinalityOfFirstDay! - 1)
98
+
99
+ //dateComponents.day = i
100
+
101
+ // ③ 表示する月の初日から②で計算した差を引いた日付を取得
102
+
103
+
104
+
105
+ let date = Calendar.current.date(byAdding: dateComponents as DateComponents, to: firstDateOfMonth() as Date)!
106
+
107
+ // ④配列に追加
108
+
109
+
110
+
111
+ currentMonthOfDates.append(date as NSDate)
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ // ⑵表記の変更
120
+
121
+ func conversionDateFormat(indexPath: IndexPath) -> String {
122
+
123
+
124
+
125
+ dateForCellAtIndexPath(numberOfItem: numberOfItems)
126
+
127
+
128
+
129
+ //
130
+
131
+ // Pickされた日付Stringの準備
132
+
133
+ //
134
+
135
+
136
+
137
+ var formatter: DateFormatter = DateFormatter()
138
+
139
+ formatter.dateFormat = "yyyy年MM月dd日"
140
+
141
+ let pickedDateString = formatter.string(from: ) //指定したformatで作成したStringを取得
142
+
143
+
144
+
145
+ if formatter == UserDefaults.standard.dictionary(forKey: "BIRTH") {
146
+
147
+ //formatter.dateFormat = "d"
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ return formatter.string(from: currentMonthOfDates[indexPath.row] as Date)
156
+
157
+
158
+
159
+ }
160
+
161
+ ```