일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
- 파이썬웹크롤링
- 유학토플
- JS
- 우리를위한프로그래밍
- 토플공부수기
- nodeJS
- 스위프트
- 파이썬
- 교환학생토플
- 토플공부
- 인프런강의
- 인프런오리지널
- Python3
- 노드JS
- 리프2기
- 토플
- IOS
- 인프런파이썬
- SwiftUI
- 인프런파이썬강의
- 프로그래머스
- 웹크롤링
- 인프런
- 자바스크립트
- swift
- 파이썬중급
- 카카오톡채팅봇
- uikit
- IOS프로그래밍
- 파이썬중급강의
- Today
- 189
- Total
- 264,188
먹고 기도하고 코딩하라
[SwiftUI] Text 뷰와 Date.FormatStyle로 날짜와 시간 표현하기 본문
날짜를 위한 텍스트 뷰를 만들기 위해, init(ClosedRange<Date>), init(DateInterval), init(Date, style: Text.DateStyle)을 사용할 수 있다. init(ClosedRange<Date>)는 두 날짜 간 localized range, init(DateInterval)은 시간 간격, 마지막 것은 날짜와 시간을 특정 스타일로 보여주는 것이다.
Text(DateInterval(start: Date(), end: Date(timeInterval: 10000, since: Date())))
Text(DateInterval(start: Date(), end: Date(timeInterval: 1000000, since: Date())))
Text.DateStyle을 줘서 생성했을 때의 모습이다.
VStack {
Text(Date(), style: .date) // 년도, 날짜 표시
Text(Date(), style: .offset) // 지금으로부터 지난 날짜
Text(Date(), style: .time) // 시간만 표시
Text(Date(), style: .relative) // 지금과 비교한 날짜
Text(Date(), style: .timer) // 지금으로부터 시간
}
포맷팅하면서 텍스트 뷰를 생성하는 방법으로 더 정교하게 시간을 표현할 수도 있다.
다음은 몇 가지 실용적인 사례들이다.
VStack {
Text(Date(), format: Date.FormatStyle(date: .long, time: .standard)) // June 28, 2022, 12:49:33 PM
Text(Date(), format: Date.FormatStyle(date: .abbreviated, time: .shortened)) // Jun 28, 2022, 12:49 PM
Text(Date(), format: Date.FormatStyle()
.year()
.month(.wide)
.day(.defaultDigits))
.weekday(.abbreviated) // Tue, June 28, 2022
Text(Date(), format: Date.FormatStyle()) // 6/28/2022, 12:49 PM
Text(Date(), format: Date.FormatStyle(date: .numeric, time: .omitted)) // 6/28/2022
Text(Date(), format: Date.FormatStyle(date: .omitted, time: .standard)) // 12:49:33 PM
Text(Date(), format: Date.FormatStyle()
.hour(.defaultDigitsNoAMPM).minute()) // 12:52
}
Date.FormatStyle은 Date 객체의 날짜와 시간을 사용자에게 보여줄 수 있도록 하는 옵션을 제공하긴 한다.
문서에서는 사용자에게 실제로 데이터를 보여줄 때는 formatted(date:time:) 메소드를 사용할 것을 권하고 있다. 필요에 따라 날짜와 시간 스타일을 다르게 줄 수 있다는 장점이 있다. (사실 사용법은 비슷하다.)
VStack(spacing: 10) {
Text(Date().formatted(date: .abbreviated, time: .standard)) // Jun 28, 2022, 7:18:59 PM
Text(Date().formatted(date: .numeric, time: .omitted)) // 6/28/2022
Text(Date().formatted(date: .omitted, time: .shortened)) // 7:18 PM
Text(Date().formatted(date: .long, time: .complete)) // June 28, 2022, 7:18:59 PM GMT+9
Text(Date().formatted(date: .complete, time: .complete)) // Tuesday, June 28, 2022, 7:18:59 PM GMT+9
Text(Date().formatted()) // 6/28/2022, 7:18 PM
}
.multilineTextAlignment(.center)
.padding()
formatted로 date, time을 주지 않을 때의 기본 date 스타일은 abbreviated이고, time 스타일은 shortened이다.
date
- .complete : Tuesday, June 28, 2022 (요일, 날짜, 년도 순)
- .long : June 28, 2022 (.complete에서 요일만 없어짐)
- .abbreviated : Jun 28, 2022 (월을 3글자로 줄인 후 날짜, 년도는 4자리)
- .numeric : 6/28/2022 (월/일/년도 순)
- .omitted : 생략
time
- .complete : 7:18:59 PM GMT+9 (시:분:초 AM/PM 표준시)
- .standard : 7:18:59 PM (표준시 표기 X)
- .shortened : 7:18 PM (초 표기 X)
- .omitted : 생략
입력 문자열로부터 Date 객체를 파싱하려면 Date.ParseStrategy(format:)을 사용할 수 있다.
let inputString = "Archive for month 8, archived on day 23 - complete."
let strategy = Date.ParseStrategy(format: "Archive for month \(month: .defaultDigits), archived on day \(day: .twoDigits) - complete.", locale: Locale(identifier: "en_US"), timeZone: TimeZone(abbreviation: "CDT")!)
if let date = try? Date(inputString, strategy: strategy) {
print(date.formatted()) // "Aug 23, 2000 at 12:00 AM"
}
입력 문자열이 제대로 됐다면 Date.FormatStyle().parse(String)을 사용할 수도 있다. 이 경우 FormatStyle()은 들어온 문자열이 지켜야 할 형식을 명시하고 있으므로 이것을 지켜줘야 한다.
let birthdayFormatStyle = Date.FormatStyle()
.year(.defaultDigits)
.month(.abbreviated)
.day(.twoDigits)
.hour(.defaultDigits(amPM: .abbreviated))
.minute(.twoDigits)
.timeZone(.identifier(.long))
.era(.abbreviated)
.weekday(.abbreviated)
let yourBirthdayString = "Mon, Feb 17, 1997 AD, 1:27 AM America/Chicago"
// Create a date instance from a string representation of a date.
let yourBirthday = try? birthdayFormatStyle.parse(yourBirthdayString)
'앱 > Swift' 카테고리의 다른 글
애플 공식 문서 Introducing SwiftUI - Landmarks (3) Handling User Input (0) | 2022.07.02 |
---|---|
애플 공식 문서 Introducing SwiftUI - Landmarks (2) Building Lists and Navigation (0) | 2022.06.30 |
[SwiftUI] Text 뷰와 Date.FormatStyle로 날짜와 시간 표현하기 (0) | 2022.06.29 |
[SwiftUI] Text 뷰와 단짝인 modifiers (0) | 2022.06.28 |
[SwiftUI] 무한적아 List 뷰 (0) | 2022.06.26 |
애플 공식 문서 Introducing SwiftUI - Landmarks (1) Creating and Combining Views (0) | 2022.06.24 |
- Tag
- date, DateFormat, swift, 스위프트