未分類

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

TableViewとは

TableViewとはリスト型のパーツである。 TableViewを構築するときに必要なメソッド ・セルの数を決めるメソッド ・セルのセクション数を決めるメソッド ・セルを構築する際に呼ばれるメソッド …

no image

tableViewでcellを長押ししないと選択できない挙動の原因

問題 tableViewで急にセルをタップしても選択されなくなった。 長押ししてみると選択状態をなった。 原因 キーボード外をタップした時に、キーボードを閉じるメソッドを実装するために、以下のコードを …

no image

xcodeでファイル名の変更をする

xcodeでファイル名の変更をする簡単な方法↓ 参考:Xcodeでファイルの名前を変更またはリファクタリングする

no image

テキストアニメ、モーションのフリーテンプレート使い方

【商用利用OK】Premiere Proのテキストアニメーション・テンプレート100種類 .mogrtのフォントがpremiere proで変更できない時の対処法

no image

オプショナル型とは

オプショナル型 var x: String? ←nilが入ってる nilを代入できる。 非オプショナル型 var x: String ←何も入ってない nilを代入できない。