0
1

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 1 year has passed since last update.

次の水曜日の Date型 を取得する

Last updated at Posted at 2022-09-16

サンプルコード

import Foundation

let calendar = Calendar(identifier: .gregorian)
let wednesdayComponent = DateComponents(weekday: 4)
let wednesday = calendar.nextDate(after: Date(), matching: wednesdayComponent, matchingPolicy: .nextTime)!

print(Date())
print(wednesday)

出力結果

2022-09-16 09:05:06 +0000
2022-09-21 00:00:00 +0000

使ったAPI

nextDate(after:matching:matchingPolicy:repeatedTimePolicy:direction:)

コメント

適当に検索するとこれらの記事が引っかかり参考にできそうと思いましたが、
Apple Developerのドキュメントを読んでいるとiOS 8からnextDate()のAPIが使えることがわかりました。
こちらを採用しました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?