init(date: Date.IntervalFormatStyle.DateStyle?, time: Date.IntervalFormatStyle.TimeStyle?, locale: Locale, calendar: Calendar, timeZone : TimeZone)
static var interval: Date.IntervalFormatStyle
Availability 有効性
Technology
struct IntervalFormatStyle
Use a date interval format style to create user-readable strings in the form of <start> - <end>
for your app’s interface, where <start>
and <end>
are date values that you supply. The format style uses locale and language information, along with custom formatting options, to define the content of the resulting string.
日付間隔書式設定スタイルを使ってユーザが読むことができる文字列を<start> - <end>
の形式であなたのアプリのインターフェイスのために作成してください、そこで<start>
と<end>
はあなたが提供する日付値です。書式設定スタイルは、ロケールと言語情報を使います、あつらえの書式設定オプションに加えて、そうして結果文字列の内容を定義します。
Date
provides a variety of localized presets and configuration options to create user-visible representations of date intervals. When displaying a date interval to a user, use the formatted(date:
instance method of Range<Date>
. Set the date and time styles of the date interval format style separately, according to your particular needs.
Date
は、様々なローカライズされたプリセットそして構成設定オプションを提供して、ユーザが見ることができる日付間隔の表現を作成します。ある日付間隔をユーザに表示する時、Range<Date>
のformatted(date:
インスタンスメソッドを使ってください。日付間隔書式設定スタイルの日付と時刻スタイルを別々に設定してください、あなたの特定の需要に応じて。
For example, to create a date interval string with a full date and no time representation, set the Date
to complete
and the Date
to omitted
.
例えば、日付間隔文字列を完全な日付で、そして時刻表現なしで作成するには、Date
をcomplete
に、そしてDate
をomitted
に設定してください。
if let today = Calendar.current.date(byAdding: .day, value: -120, to: Date()),
let thirtyDaysBeforeToday = Calendar.current.date(byAdding: .day, value: -30, to: today) {
// today: Mar 1, 2021 at 8:01 PM
// thirtyDaysBeforeToday: Jan 30, 2021 at 8:01 PM
// Create a Range<Date>.
let last30days = thirtyDaysBeforeToday..<today
print(last30days.formatted(date: .complete, time: .omitted))
// Saturday, January 30 – Monday, March 1, 2021
}
You can create string representations of date intervals with various levels of brevity using a variety of preset date and time styles. The following example shows date styles of long
, abbreviated
, and numeric
, and time styles of shortened
, standard
, and complete
:
あなたは、日付間隔の文字列表現の作成を、さまざまな簡潔さの水準で、多様なあらかじめ設定された日付と時刻スタイルを使って行えます。以下の例は、long
、abbreviated
、そしてnumeric
の日付スタイル、そしてshortened
、standard
、そしてcomplete
の時刻スタイルを示します:
if let today = Calendar.current.date(byAdding: .day, value: -120, to: Date()),
let thirtyDaysBeforeToday = Calendar.current.date(byAdding: .day, value: -30, to: today) {
// today: Mar 1, 2021 at 8:01 PM
// thirtyDaysBeforeToday: Jan 30, 2021 at 8:01 PM
// Create a Range<Date>.
let last30days = thirtyDaysBeforeToday..<today
print(last30days.formatted(date: .long, time: .shortened))
// January 30, 2021, 8:01 PM – March 1, 2021, 8:01 PM
print(last30days.formatted(date: .abbreviated, time: .standard))
// Jan 30, 2021, 8:01:49 PM – Mar 1, 2021, 8:01:49 PM
print(last30days.formatted(date: .numeric, time: .complete))
// 1/30/2021, 8:01:49 PM CST – 3/1/2021, 8:01:49 PM CST
print(last30days.formatted())
// 1/30/21, 8:01 PM – 3/1/21, 8:01 PM
}
The default date style is abbreviated
and the default time style is shortened
.
省略時の日付スタイルは、abbreviated
です、そして省略時の時刻スタイルは、shortened
です。
For full customization of the string representation of a date interval, use the formatted(_:)
instance method of Range<Date>
and provide a Date
instance.
日付間隔の文字列表現の完全なカスタマイズのために、Range<Date>
のformatted(_:)
インスタンスメソッドを使ってください、そしてDate
インスタンスを提供してください。
You can achieve any customization of date and time representation your app requires by appying a series of convenience modifiers to your format style. The following example applies a series of modifiers to the format style to precisely define the formatting of the year, month, day, hour, minute, and time zone components of the resulting string: あなたは、あなたのアプリが必要とする日付と時刻表現の何らかのカスタマイズをアーカイブすることが、一連の便宜修飾子をあなたの書式設定スタイルに適用することによって可能です。以下の例は、一連の修飾子を書式設定スタイルに適用することで、結果文字列の年、月、日にち、時、分、そしてタイムゾーン構成要素の書式設定を正確に定義します。
if let today = Calendar.current.date(byAdding: .day, value: -140, to: Date()),
let sevenDaysAfterToday = Calendar.current.date(byAdding: .day, value: 7, to: today) {
// Create a Range<Date>.
let weekFromNow = today..<sevenDaysAfterToday
// Call the .formatted method on a Range<Date> and pass in an instance of Date.IntervalFormatStyle.
weekFromNow.formatted(
Date.IntervalFormatStyle()
.year()
.month(.abbreviated)
.day()
.hour(.defaultDigits(amPM: .narrow))
.weekday(.abbreviated)
) // Wed, Feb 10, 2021, 3 p – Wed, Feb 17, 2021, 3 p
}
Date
provides a convenient factory variable, interval
, to shorten the syntax when applying date and time modifiers to customize the format.
Date
は、便宜ファクトリ変数interval
を提供します、そうして日付と時刻修飾子それらを適用して書式設定をカスタマイズする時に、構文を短くします。
if let today = Calendar.current.date(byAdding: .day, value: -140, to: Date()),
let sevenDaysBeforeToday = Calendar.current.date(byAdding: .day, value: -7, to: today) {
// Create a Range<Date>.
let weekBefore = sevenDaysBeforeToday..<today
let localeArray = ["en_US", "sv_SE", "en_GB", "th_TH", "fr_BE"]
for localeID in localeArray {
// Call the .formatted method on a Range<Date> and pass in an instance of Date.IntervalFormatStyle.
print(weekBefore.formatted(.interval
.day()
.month(.wide)
.weekday(.short)
.hour(.conversationalTwoDigits(amPM: .wide))
.locale(Locale(identifier: localeID))))
}
}
// We, February 3, 3 PM – We, February 10, 3 PM
// on 3 februari 15 – on 10 februari 15
// We 3 February, 15 – We 10 February, 15
// พ. 3 กุมภาพันธ์ 15 – พ. 10 กุมภาพันธ์ 15
// me 3 février, 15 h – me 10 février, 15 h
init(date: Date.IntervalFormatStyle.DateStyle?, time: Date.IntervalFormatStyle.TimeStyle?, locale: Locale, calendar: Calendar, timeZone : TimeZone)
static var interval: Date.IntervalFormatStyle
func timeZone (Date.IntervalFormatStyle.Symbol.TimeZone) -> Date.IntervalFormatStyle
func locale(Locale) -> Date.IntervalFormatStyle
var calendar: Calendar
var locale: Locale
var timeZone : TimeZone
func day() -> Date.IntervalFormatStyle
func hour(Date.IntervalFormatStyle.Symbol.Hour) -> Date.IntervalFormatStyle
func minute() -> Date.IntervalFormatStyle
func month(Date.IntervalFormatStyle.Symbol.Month) -> Date.IntervalFormatStyle
func second() -> Date.IntervalFormatStyle
func weekday(Date.IntervalFormatStyle.Symbol.Weekday) -> Date.IntervalFormatStyle
func year() -> Date.IntervalFormatStyle
func format(Range<Date>) -> String
init(from: Decoder)
func encode(to: Encoder)
var hashValue : Int
func hash(into: inout Hasher)
static func != (Date.IntervalFormatStyle, Date.IntervalFormatStyle) -> Bool
static func == (Date.IntervalFormatStyle, Date.IntervalFormatStyle) -> Bool
typealias Date.IntervalFormatStyle.DateStyle
typealias Date.IntervalFormatStyle.FormatInput
typealias Date.IntervalFormatStyle.FormatOutput
typealias Date.IntervalFormatStyle.Symbol
typealias Date.IntervalFormatStyle.TimeStyle