5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift カレンダー,日付系備忘録

Last updated at Posted at 2015-12-16

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
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?