未分類

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

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

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

no image

UserDefaultsとは

UserDefaultsとは UserDefaultsを使えばアプリ内にデータを保存することができる。 アプリを端末から消去しない限りずっと残り続ける。 入力途中のものやメモの保存に役立つ。 保存した …

no image

配列の作り方

配列の作り方 ・宣言する var abcdBox = [String]() ・宣言時に配列の要素を入れておく var abcdBox = [“a”,”b”,”c”,”d”] ・要素を1つだけ追加する …

no image

StaticCellでコードから特定のセルを選択状態にしたい時

セルを選択したいタイミングに以下のコードを書く。 sectionとrowでセルを指定する。 tableView.selectRow(at: IndexPath(row: 0, section: 1), …

no image

TextViewにPlaceholderを追加する簡単な方法

TextViewはTextFieldのようにPlaceholderを設定できないため、自分で実装する必要がある。 ここでは、TextViewの上にLabelを設置し、TextViewの入力判定を行い、 …