Instances of DateFormatter create string representations of NSDate objects, and convert textual representations of dates and times into NSDate objects. For user-visible representations of dates and times, DateFormatter provides a variety of localized presets and configuration options. For fixed format representations of dates and times, you can specify a custom format string.DateFormatterのインスタンスはNSDateオブジェクトの文字列表現を作成します、そして日付と時刻のテキスト表現をNSDateオブジェクトへと変換します。ユーザが見ることができる日付と時刻の表現に対して、DateFormatterは様々なローカライズされた事前設定や構成設定オプションを提供します。日付と時刻の固定形式の表現に対して、あなたは誂えの書式設定文字列を指定できます。
let dateFormatter =DateFormatter()dateFormatter.dateStyle = .mediumdateFormatter.timeStyle = .nonelet date =Date(timeIntervalSinceReferenceDate: 118800)// US English Locale (en_US)dateFormatter.locale =Locale(identifier: "en_US")print(dateFormatter.string(from: date)) // Jan 2, 2001// French Locale (fr_FR)dateFormatter.locale =Locale(identifier: "fr_FR")print(dateFormatter.string(from: date)) // 2 janv. 2001// Japanese Locale (ja_JP)dateFormatter.locale =Locale(identifier: "ja_JP")print(dateFormatter.string(from: date)) // 2001/01/02
If you need to define a format that cannot be achieved using the predefined styles, you can use the setLocalizedDateFormatFromTemplate(_:) to specify a localized date format from a template.
あらかじめ定義されたスタイルを使ってアーカイブできない書式設定を定義するのをあなたが望むならば、あなたはsetLocalizedDateFormatFromTemplate(_:)を使って、ローカライズされた日付書式設定を雛形から指定します。
let dateFormatter =DateFormatter()let date =Date(timeIntervalSinceReferenceDate: 410220000)// US English Locale (en_US)dateFormatter.locale =Locale(identifier: "en_US")dateFormatter.setLocalizedDateFormatFromTemplate("MMMMd") // set template after setting localeprint(dateFormatter.string(from: date)) // December 31// British English Locale (en_GB)dateFormatter.locale =Locale(identifier: "en_GB")dateFormatter.setLocalizedDateFormatFromTemplate("MMMMd") // // set template after setting localeprint(dateFormatter.string(from: date)) // 31 December
Working With Fixed Format Date Representations
固定された書式設定日付表現を扱う
When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale ("en_US_POSIX"), and set the timeZone property to UTC.
固定された書式設定日付を扱う場合、例えばRFC 3339など、あなたはdateFormatプロパティを設定して、書式設定文字列を指定します。最も固定された書式設定のために、あなたはまたlocaleプロパティをPOSIXロケール("en_US_POSIX")に設定すべきです、そしてtimeZoneプロパティをUTCに設定すべきです。
letRFC3339DateFormatter=DateFormatter()RFC3339DateFormatter.locale =Locale(identifier: "en_US_POSIX")RFC3339DateFormatter.dateFormat ="yyyy-MM-dd'T'HH:mm:ssZZZZZ"RFC3339DateFormatter.timeZone =TimeZone(secondsFromGMT: 0)/* 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time) */let string ="1996-12-19T16:39:57-08:00"let date =RFC3339DateFormatter.date(from: string)
On iOS 7 and later NSDateFormatter is thread safe.
iOS 7以降ではNSDateFormatterはスレッド安全です。
In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.
macOS 10.9以降ではNSDateFormatterはスレッド安全です、あなたがモダンな挙動を64-bitアプリにおいて使っている限りは。
On earlier versions of the operating system, or when using the legacy formatter behavior or running in 32-bit in macOS, NSDateFormatter is not thread safe, and you therefore must not mutate a date formatter simultaneously from multiple threads.
オペレーティングシステムの以前のバージョンでは、またはレガシーフォーマッタ挙動を使うまたは32-bitをmacOSで実行する場合、NSDateFormatterはスレッド安全ではありません、そしてあなたはそのゆえに複数のスレッドから同時に日付フォーマッタを変更してはいけません。
Returns a date representation of a specified string that the system interprets using the receiver’s current settings.
指定された文字列の日付表現を返します、それはシステムがレシーバのもつ現在の設定を使って解釈するものです。
Returns a string representation of a specified date that the system formats using the receiver’s current settings.
指定された日付の文字列表現を返します、それはシステムがレシーバのもつ現在の設定を使って書式設定します。
Returns a string representation of a specified date, that the system formats for the current locale using the specified date and time styles.
指定された日付の文字列表現を返します、それはシステムが現在のロケールに対してその指定された日付と時刻のスタイルを使って書式設定します。
Returns by reference a date representation of a specified string and its date range, as well as a Boolean value that indicates whether the system can parse the string.
指定された文字列とそれの日付範囲の日付表現を参照によって返します、それだけでなくあるブール値も、それはシステムがその文字列を構文解析できるかどうかを指し示すものです。
Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.
指定されたロケールに対して適切に整えられた、与えられた日付書式設定構成要素を表している、ローカライズされた日付書式設定文字列を返します。
A Boolean value that indicates whether the receiver uses phrases such as “today” and “tomorrow” for the date component.
レシーバが語句、例えば “today” や “tomorrow” を日付構成要素に対して使うかどうかを指し示すブール値。