Class

DateFormatter

A formatter that converts between dates and their textual representations. 日付とそれらのテキスト表現の間で変換をするフォーマッタ。

Declaration 宣言

class DateFormatter : Formatter

Overview 概要

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は様々なローカライズされた事前設定や構成設定オプションを提供します。日付と時刻の固定形式の表現に対して、あなたは誂えの書式設定文字列を指定できます。

When working with date representations in ISO 8601 format, use ISO8601DateFormatter instead. 日付表現をISO 8601フォーマットにおいて扱う場合、ISO8601DateFormatterを代わりに使ってください。

To represent an interval between two NSDate objects, use DateIntervalFormatter instead. 2つのNSDateオブジェクトの間の隔たりを表すには、DateIntervalFormatterを代わりに使ってください。

To represent a quantity of time specified by an NSDateComponents object, use DateComponentsFormatter instead. NSDateComponentsオブジェクトによって指定された時間量を表すには、代わりにDateComponentsFormatterを使ってください。

Working With User-Visible Representations of Dates and Times ユーザが見ることができる日付と時刻の表現を扱う

When displaying a date to a user, you set the dateStyle and timeStyle properties of the date formatter according to your particular needs. For example, if you want to show the month, day, and year without showing the time, you would set the dateStyle property to DateFormatter.Style.long and the timeStyle property to DateFormatter.Style.none. Conversely, if you want to show only the time, you would set the dateStyle property to DateFormatter.Style.none and the timeStyle property to DateFormatter.Style.short. Based on the values of the dateStyle and timeStyle properties, DateFormatter provides a representation of a specified date that is appropriate for a given locale. 日付をユーザに表示する場合、あなたは日付フォーマッタのdateStyletimeStyleプロパティをあなたの特定の必要に応じて設定します。例えば、あなたが月、日、そして年の表示を、時間表示なしに望むならば、あなたはdateStyleプロパティをDateFormatter.Style.longに、timeStyleプロパティをDateFormatter.Style.noneに設定するでしょう。逆に言えば、あなたが時間だけを示すことを望むならば、あなたはdateStyleプロパティをDateFormatter.Style.noneに、そしてtimeStyleプロパティをDateFormatter.Style.shortに設定するでしょう。dateStyletimeStyleプロパティの値に基づいて、DateFormatterは指定された日付の、与えられたロケールにふさわしい表現を提供します。


let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
 
let 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 locale
print(dateFormatter.string(from: date)) // December 31
 
// British English Locale (en_GB)
dateFormatter.locale = Locale(identifier: "en_GB")
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMd") // // set template after setting locale
print(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に設定すべきです。


let RFC3339DateFormatter = 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)

For more information, see Technical Q&A QA1480 “NSDateFormatter and Internet Dates”. さらなる情報のために、Technical Q&A QA1480 “NSDateFormatter and Internet Dates”を見てください。

Thread Safety スレッド安全

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はスレッド安全ではありません、そしてあなたはそのゆえに複数のスレッドから同時に日付フォーマッタを変更してはいけません。

Topics 話題

Converting Objects オブジェクト変換

Managing Formats and Styles 書式設定とスタイルを管理する

Managing Attributes 属性を管理する

Managing Behavior Version 挙動バージョンを管理する

Managing Natural Language Support 自然言語サポートを管理する

Managing AM and PM Symbols AM および PM表象を管理する

Managing Weekday Symbols 曜日表象を管理する

Managing Month Symbols 月表象を管理する

Managing Quarter Symbols 四半期表象を管理する

Managing Era Symbols 紀元表象を管理する

Constants 定数

Instance Properties インスタンスプロパティ

Relationships 関係

Inherits From 継承元

See Also 参照

Date Formatting 日付を書式設定する