본문 바로가기

IOS 개발

[iOS] DateFormatter

 

# Class

# DateFormatter

 

날짜와 텍스트 표현 간에 변환되는 형식입니다.

A formatter that converts between dates and their textual representations.

 

 

선언

Declaration

class DateFormatter : Fomatter

 

 

개요

Overview

 

DateFormatter 인스턴스는 NSDate 객체의 문자열 표현을 만들고 날짜 및 시간의 텍스트 표현을 NSDate 객체로 변환합니다. 사용자가 볼 수있는 날짜 및 시간 표현을 위해 DateFormatter는 다양한 현지화 된 사전 설정 및 구성 옵션을 제공합니다. 날짜 및 시간의 고정 형식 표시를 위해 사용자 정의 형식 문자열을 지정할 수 있습니다.

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.

 

 

ISO 8601 형식의 날짜 표현으로 작업할 때는 ISO8601DateFormatter를 대신 사용합니다.

When working with date representations in ISO 8601 format, use # ISO8601DateFormatter # instead.

 

 

 

두 NSDate 개체 간의 간격을 나타내려면 대신 DateIntervalFormatter를 사용합니다.

NSDateComponents 개체가 지정한 시간을 나타내려면 대신 DateComponentsFormatter를 사용합니다.

To represent an interval between two  NSDate objects, use # DateIntervalFormatter # instead.

To represent a quantity of time specified by an NSDateComponents object, use # DateComponentsFormatter # instead.

 

 

사용자가 볼 수있는 날짜 및 시간 표현 작업

Working With User-Visible Representations of Dates and Times

 

사용자에게 날짜를 표시 할 때 특정 요구에 따라 날짜 형식의 dateStyle 및 timeStyle 속성을 설정합니다.

예를 들어, 시간을 표시하지 않고 월, 일 및 연도를 표시하려면 dateStyle 속성을  DateFormatter.Style.long 으로  설정하고 timeStyle 속성을  DateFormatter.Style.none  으로  설정합니다. 

반대로 시간 만 표시하려면 dateStyleproperty를 DateFormatter.Style.none으로 설정하고 timeStyle 속성을 DateFormatter.Style.short로 설정합니다. 

dateStyle 및 timeStyle 속성 값을 기반으로 DateFormatter는 지정된 locale에 적합한 지정된 날짜의 표현을 제공합니다.

When displaying a date to a user, you set the dateStyle and timeStyleproperties 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 dateStyleproperty 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.

 

 

 

미리 정의된 스타일을 사용하여 달성할 수 없는 형식을 정의해야 하는 경우

setLocalizedDateFormatFromTemplate(_:)를 사용하여 template 에서 현지화된 날짜 형식을 지정할 수 있습니다.

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.

 

고정 형식 날짜 표현 작업

Working With Fixed Format Date Representations

 

===========================

중요

Important

macOS 10.12 이상 또는 iOS 10 이상에서는 ISO 8601 날짜 표시로 작업 할 때 ISO8601DateFormatter 클래스를 사용하십시오.

In macOS 10.12 and later or iOS 10 and later, use the ISO8601DateFormatter class when working with ISO 8601 date representations.

============================

 

RFC 3339와 같은 고정 형식 날짜로 작업할 때 형식 문자열을 지정하도록 dateFormat 속성을 설정합니다.

대부분의 고정 형식의 경우, locale 속성을 POSIX locale로 설정하고("en_US_POSIX") 타임존 속성을 UTC로 설정해야 합니다.

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.

 

 

자세한 내용은 기술 Q&A QA1480 "NSDateFormatter 및 인터넷 날짜"를 참조하시기 바랍니다.

For more information, see # Technical Q&A QA1480 "NSDateFormatter and Internet Dates". #

 

 

 

 

Thread 안전

Thread Safety

 

iOS 7 이상에서 NSDateFormatter는 thread로부터 안전합니다.

On iOS 7 and later NSDateFormatter is thread safe.

 

 

macOS 10.9 이상에서 NSDateFormatter는 64 비트 앱에서 최신 동작을 사용하는 한 스레드로부터 안전합니다.

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에서 32 비트로 실행하는 경우 

NSDateFormatter는 스레드로부터 안전하지 않으므로 여러 스레드에서 동시에 날짜 포맷터를 변경하지 않아야합니다.

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.

 

 

 

 

 

 

참조 : 애플공식문서 Developer Documentation