개발 창고/iOS
[SwiftUI] Date에서 특정 값 가져오는 방법
로이제로
2023. 8. 4. 22:00
반응형
※ 이 글은 Swift 5 기준으로 작성되었습니다.
1. 오늘 날짜 출력
2. 날짜 계산 문법 (byAdding)
Calendar.current.date(byAdding:날짜기준, value:날짜값, to:기준일자)
만약 어제 일자 정보를 가져오려고 한다면
날짜기준 : .day
날짜값 : -1
기준일자 : today (or Date())
3. 날짜기준
주요 날짜 정보는 아래의 애플 개발자 사이트에서 참고하실 수 있습니다.
https://developer.apple.com/documentation/foundation/calendar/component
Calendar.Component | Apple Developer Documentation
An enumeration for the various components of a calendar date.
developer.apple.com
여기서는 자주 쓰는 것 기준으로 말씀드리면,
(예시 : 날짜를 2023-07-17 23:51:11 +0000 기준으로 설명
case | description | example |
.year | 연도 (Identifier for the year unit.) | 2023 |
.month | 월 (Identifier for the month unit.) | 07 |
.day | 일 (Identifier for the day unit.) | 17 |
.weekOfYear | 년 기준 주차. 52주차 기준 (Identifier of the week of the year unit.) | |
.weekOfMonth | 월 기준 주차 (Identifier for the week of the month calendar unit.) | |
.weekday | 요일 (Identifier for the weekday unit.) | |
.hour | 시간 (Identifier for the hour unit.) | 23 |
.minute | 분 (Identifier for the minute unit.) | 51 |
.second | 초 (Identifier for the second unit.) | 11 |
테스트 소스
See the Pen [SwiftUI] Date에서 특정 값 가져오는 방법 by 장진용 (@nvwgjloa-the-animator) on CodePen.
반응형