Swift2.0と1系でなんか違うので備忘録。
まだまだあるので,必要に応じて適宜追記していく。
※ Playground レベルでしかみてない。
Swift
import UIKit
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
// NSDate() でもいいけど訳あり・・・
var date = calendar.dateWithEra(1, year: 2015, month: 12, day: 1, hour: 0, minute: 0, second: 0, nanosecond: 0)!
// Swift 2系から?
let unitFlags: NSCalendarUnit = [ .Year,
.Month,
.Day,
.Weekday,
.Hour,
.Minute,
.Second ]
let year = calendar.component(.Year, fromDate: date)
let month = calendar.component(.Month, fromDate: date)
let day = calendar.component(.Day, fromDate: date)
String(year) + "年" + String(month) + "月" + String(day) + "日"
// 何曜か(weekdayって1からだったっけ?)
var components = calendar.components(unitFlags, fromDate:date)
var weekDay = components.weekday
// おまけ
let dayArray = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var nowDay = dayArray[weekDay-1]
// 指定月は何日あるか
var range : NSRange = calendar.rangeOfUnit(.Day, inUnit:.Month , forDate:date)
var last = range.length;
"Dec 1, 2015, 12:00 AM"
"2015年12月1日"
3
"Tue"
31