LoginSignup
4
5

More than 5 years have passed since last update.

【swift 4】 2つの日付の差が何日かを整数で取得する。

Last updated at Posted at 2018-11-11

【swift 4】 2つの日付の差が何日かを整数で取得します。

let myDateFormatter = DateFormatter()
myDateFormatter.dateFormat = "yyyy年M月d日"
let day1:Date = myDateFormatter.date(from: "2018年11月29日")!
let day2:Date = myDateFormatter.date(from: "2018年12月2日")!
let dayInterval = (Calendar.current.dateComponents([.day], from: day1, to: day2)).day
print(dayInterval!)

print結果は 3 です。
day1 と day2 の日付を入れ替えると、結果は -3 になります。

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