質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

827閲覧

swiftで曜日(EEEE)を用いて条件分岐する仕方について

hirobot777

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/05/06 13:02

前提・実現したいこと

swiftで今日の曜日を取得して、(月曜日〜金曜日なら平日)(土・日曜日なら休日)と出力するコードを書こうとしました。
しかし、Saturdayと、Sundayの定義の仕方が分からないです。
アドバイスお待ちしてます。

発生している問題・エラーメッセージ

Cannot find 'Saturday' in scope Cannot find 'Sunday' in scope

該当のソースコード

swift5

1 let date = Date() 2 let formatter = DateFormatter() 3 formatter.timeZone = .current 4 formatter.locale = .current 5 formatter.dateFormat = "EEEE" 6 let DATE:String = formatter.string(from: date) 7 8 if DATE = Sunday && Saturday { 9 label.text = "休日" 10 }else{ 11 label.text = "平日" 12 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/05/06 13:13

"EEE" でフォーマットすると文字列になるので、文字列比較する必要がありますが、端末の設定を日本語以外にしてると破綻しますね…。
tomato879241

2021/05/06 14:02

CalendarのdateComponentsの使い方を学んで、「weekday」のcomponentを取得してください。
guest

回答1

0

週末かどうかを判定したいのであれば、isDateInWeekend(_:)を使えば良いのではないかと。

isDateInWeekend(_:) | Apple Developer Documentation

isDateInWeekend(_:)

Returns a Boolean value indicating whether the given date is within a weekend period.

Declaration

func isDateInWeekend(_ date: [Date](https://developer.apple.com/documentation/foundation/date)) -> [Bool](https://developer.apple.com/documentation/swift/bool)

Parameters

date

The specified date.

Return Value

true if the given date is within a weekend, as defined by the calendar and calendar’s locale; otherwise, false.

swift

1let calendar = Calendar(identifier: .gregorian) 2let formatter = ISO8601DateFormatter() 3formatter.timeZone = TimeZone(abbreviation: "JST") 4let date = Date() 5print(formatter.string(from: date)) 6print(calendar.isDateInWeekend(date)) 7 8guard let may05 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 5)) else {return} 9print(formatter.string(from: may05)) // 2021-05-05T00:00:00+09:00 10print(calendar.isDateInWeekend(may05)) // false 11 12guard let may08 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 8)) else {return} 13print(formatter.string(from: may08)) // 2021-05-08T00:00:00+09:00 14print(calendar.isDateInWeekend(may08)) //true 15 16guard let may09 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 9)) else {return} 17print(formatter.string(from: may09)) // 2021-05-09T00:00:00+09:00 18print(calendar.isDateInWeekend(may09)) // true 19 20guard let may10 = calendar.date(from: DateComponents(year: 2021, month: 5, day: 10)) else {return} 21print(formatter.string(from: may10)) // 2021-05-10T00:00:00+09:00 22print(calendar.isDateInWeekend(may10)) // false 23

formatterは出力用です。判定には不要です。もちろん祝日には非対応です。

投稿2021/05/06 16:18

izkn

総合スコア1698

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問