未分類

ordinality(of:in:for:)で週の何日目かを求める

投稿日:

ordinality(of:in:for:)

指定された大きな要素内(1週間など)の小さな要素(1日など)の序数を求める。

x月x日はその週の何番目の日付になるかがわかる。

import Foundation

let date = Date()

//今日の週の中の序数
let ordinalDay = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: date)!

//8月15日の週の中の序数
let date2 = Calendar.current.date(from: DateComponents(year: 2020, month: 8, day: 15))
let ordinalDay2 = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: date2!)

//8月16日の週の中の序数
let date3 = Calendar.current.date(from: DateComponents(year: 2020, month: 8, day: 16))
let ordinalDay3 = Calendar.current.ordinality(of: .day, in: .weekOfMonth, for: date3!)

print("今日は週の\(ordinalDay)日目です")
print("8月15日は週の\(ordinalDay2!)日目です")
print("8月16日は週の\(ordinalDay3!)日目です")

null

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

画面遷移する時に値を渡す

1.遷移先で値を受け取るための変数を宣言しておく。 class NextViewController: UIViewController { @IBOutlet weak var label2: UI …

no image

“?”の意味。オプショナルチェイニング

navigationController?となっていたら、navigationControllerがあるかないかわからない、なかったらこれより先に進んではいけないという意味。 override fun …

no image

AVPlayer()で動画を使う方法

AVPlayer()で動画を扱える。 1.AVFoundationをインポートする。 import AVFoundation 2.コードを書く //インスタンス生成 var player = AVPl …

no image

Int型からString型へ変換

Int型→String型またはString型→Int型に変換(キャスト)する方法。 //String型->Int型 let yearString = “2020” let yearInt = I …

描画モードの使い分け

描画モード(合成モード)の大まかな使い分け グレー線で大まかに区切られている。 上から、 通常:上からベタ置き 比較(暗)グループ:暗く合成される 加算グループ:明るく合成される オーバーレイグループ …