9
6

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.

Swift4の日付日時関連メモ

Posted at

appleのDateのドキュメント
https://developer.apple.com/documentation/foundation/date

###今の日時
init()

let nowDate = Date() 

###現在時刻からhoge秒

init(timeIntervalSinceNow: TimeInterval)

昨日の日時

let yesterday = Date(timeIntervalSinceNow: -60 * 60 * 24)

###指定した時刻からhoge秒

さらに昨日(一昨日)
init(timeInterval: TimeInterval, since: Date)

let dayBeforeYesterday = Date(timeInterval: -60 * 60 * 24, since: yesterday)

###2001年1月1日00:00:00 UTCを基準に初期化された日付
UTC(協定世界時:Coordinated universal time)

init(timeIntervalSinceReferenceDate: TimeInterval)

###1970年1月1日00:00:00 UTCを基準に初期化された日付値を、指定された秒数で作成します。

init(timeIntervalSince1970: TimeInterval)

let current = Date(timeIntervalSince1970: 60)

##比較

compareなど使用せずに不等号で比較できます。(Swift3から

if nowDate > yesterday {
	// 入る
}

if Date(timeIntervalSinceNow: 0) == Date() {
	//同じ
}
9
6
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
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?