[Swift] Date.formatted()

dateFormatter를 쓰려고 했더니 Date 안에 formatted()라는 method가 있더라. 공식 문서의 정의는 아래와 같다. 

 

측량에 대한 단위를 사용하고자 할 때 formatted() method를 사용할 수 있다고 한다.

 

dateFormat 관련하여 사용하는 줄로만 알았는데, 공식 문서의 예시로는 

 

 

let string = Measurement<UnitTemperature>(value: 38, unit: .celsius).formatted()
// For locale: en_US: 100.4°F

 

이런 식으로 사용하는 게 디폴트? 인 것 같다.

정먈 측량 단위를 형식화하는 느낌으로.... 

 

아무튼!

 

 

Date 구조체 안에도 formatted()라는 method가 있는데, date와 time에 대한 형식을 미리 정해진 타입으로 넣어줄 수 있다.

그 타입을 알아보자면 다음과 같다.

 

// dateFormat
let date = Date()
print(date)
print(date.formatted())
print(date.formatted(date: .long, time: .standard))
print(date.formatted(date: .complete, time: .standard))
print(date.formatted(date: .abbreviated, time: .standard))
print(date.formatted(date: .numeric, time: .standard))
print(date.formatted(date: .omitted, time: .standard))

print("=================================")

print(date.formatted(date: .complete, time: .standard))
print(date.formatted(date: .complete, time: .complete))
print(date.formatted(date: .complete, time: .omitted))
print(date.formatted(date: .complete, time: .shortened))
2023-07-25 00:20:36 +0000
7/25/2023, 9:20 AM
July 25, 2023 at 9:20:36 AM
Tuesday, July 25, 2023 at 9:20:36 AM
Jul 25, 2023 at 9:20:36 AM
7/25/2023, 9:20:36 AM
9:20:36 AM
=================================
Tuesday, July 25, 2023 at 9:20:36 AM
Tuesday, July 25, 2023 at 9:20:36 AM GMT+9
Tuesday, July 25, 2023
Tuesday, July 25, 2023 at 9:20 AM

 

추후에 보고 원하는 형식으로 나누어 사용하면 될 것 같다!

 

 

 

 

 

 

 

 

https://developer.apple.com/documentation/foundation/dateformatter

 

DateFormatter | Apple Developer Documentation

A formatter that converts between dates and their textual representations.

developer.apple.com

https://stackoverflow.com/questions/35700281/how-do-i-convert-a-date-time-string-into-a-different-date-string

 

How do I convert a date/time string into a different date string?

How will I convert this datetime from the date? From this: 2016-02-29 12:24:26 to: Feb 29, 2016 So far, this is my code and it returns a nil value: let dateFormatter = NSDateFormatter()

stackoverflow.com

https://velog.io/@minsson/Swift-Date-DateFormatter

 

[Swift] Date, DateFormatter

날짜와 시간의 한 지점을 나타내는 타입 (텍스트로 표현하고 싶다면 DateFormatter를 사용해야 한다.) 현재 날짜와 시간, Date의 다양한 이니셜라이저, ⏺ DateFormatter | 날짜 <-> String, 날짜와 String을 서

velog.io

https://developer.apple.com/documentation/foundation/measurement/3816388-formatted

 

formatted() | Apple Developer Documentation

Generates a locale-aware string representation of a measurement using the default measurement format style.

developer.apple.com

https://developer.apple.com/documentation/foundation/date

 

Date | Apple Developer Documentation

A specific point in time, independent of any calendar or time zone.

developer.apple.com

 

'iOS' 카테고리의 다른 글

[Swift] instance / Type  (0) 2023.08.01
[Swift] Enum  (0) 2023.07.30
[UIKit] UITableViewController  (0) 2023.07.27
[UIKit] Button의 title은 setTitle로 설정하자  (2) 2023.07.26
[Error] UITapGestureRecognizer issue  (0) 2023.07.23