回答編集履歴

1

出力設定追加

2015/12/09 04:35

投稿

izkn
izkn

スコア1698

test CHANGED
@@ -38,4 +38,52 @@
38
38
 
39
39
  var yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -1, toDate: currentDate, options: NSCalendarOptions.init(rawValue: 0))
40
40
 
41
+
42
+
43
+ // 追記
44
+
45
+ // 昨日の曜日取得 日曜日:1, 土曜日:7
46
+
47
+ let weekdayComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Weekday], fromDate: yesterday!)
48
+
49
+ let weekday: Int = weekdayComponents.weekday
50
+
51
+
52
+
53
+ // 出力用書式設定
54
+
55
+ let outputFormatter = NSDateFormatter()
56
+
57
+ outputFormatter.locale = NSLocale(localeIdentifier: "ja")
58
+
59
+
60
+
61
+ // outputFormatter.shortWeekdaySymbols
62
+
63
+ // ["日", "月", "火", "水", "木", "金", "土"]
64
+
65
+ // 日曜日:0, 土曜日:6
66
+
67
+ let weekdaySymbolsIndex = weekday - 1;
68
+
69
+ outputFormatter.dateFormat = "yyyy年MM月dd日(\(outputFormatter.shortWeekdaySymbols[weekdaySymbolsIndex]))"
70
+
71
+
72
+
73
+ // "2015年12月08日(火)\n"
74
+
75
+ print(outputFormatter.stringFromDate(yesterday!))
76
+
77
+
78
+
79
+ // おまけ "2015年12月8日火曜日\n"
80
+
81
+ // weekdaySymbolsIndex と outputFormatter.dateFormat は不要で一気に出力できる
82
+
83
+ outputFormatter.timeStyle = .NoStyle
84
+
85
+ outputFormatter.dateStyle = .FullStyle
86
+
87
+ print(outputFormatter.stringFromDate(yesterday!))
88
+
41
89
  ```