未分類

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

date(byAdding:to:options:)で日付を操作する

date(byAdding:to:options:) 指定された日付に、指定された要素を追加して、新たな日付を返す。 とある日付に任意の日数分足した日付を求められる。 import Foundatio …

no image

collectionViewで均等なマス目を作る

セル同士を0.5の線で区切りたいが、所々太くなってしまっている。 ボーダーを非表示にすると、わずかな隙間ができている。 セル同士の隙間を0にすることで、綺麗な0.5のボーダーにしたい。 //セルの大き …

PremierePro プロジェクトパネルの小窓を消す

左上にあるプレビューを表示する小窓を消す方法。 プロジェクトパネルのタブの3本線→「プレビューエリア」のチェックを外す。

no image

viewWillAppearについて

super.viewWillAppear(animated)は自分で書く必要あり。 override func viewWillAppear(_ animated: Bool) { super.vie …

no image

APIとは

APIとは、自分がソフトウェアの一部を公開して、他のソフトウェアと機能を共有できるようにしたもの。 こんなイメージ: 企業<うちのサイトの機能の一部を使ってもいいよ。機能を取得するための仕様書を用意し …