LoginSignup
1
2

More than 3 years have passed since last update.

swift 誕生日から今日まで何日目か?

Posted at

swiftのカレンダーアプリ作成でUICorectionViewで縦にスクロールしてカレンダーを表示する際に対応する開始日付から今日までの日数によって自動で当日へスクロールさせる時に下記の計算が必要だったので投稿します。他に良い方法があれば御伝授頂きたいとおもいます。よろしくお願いします。

let dateFormater = DateFormatter()
dateFormater.locale = Locale(identifier: "ja_JP")
dateFormater.dateFormat = "yyyy/MM/dd HH:mm:ss"
let date1 = dateFormater.date(from: "2015/10/01 12:12:12")
let date2 = Date()
let elapsedDays = Calendar.current.dateComponents([.day], from: date1!, to: date2).day
print(elapsedDays)
1
2
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
1
2