Date()
https://developer.apple.com/documentation/foundation/date
Apple Developer Documentation
developer.apple.com
- swift에서는 struct Date 를 이용해 날짜와 시간을 다룰 수 있다.
- date value 는 어떠한 시점의 날짜와 시간 정보를 가지고 있다.
- struct Date를 이용해 날짜를 비교하거나, 두 날짜 사이의 시간 간격을 구하거나, 특정 날짜에서 3일이 지나면 날짜가 어떻게 되는지 등을 구할 수 있다.
- Date()을 통해 date value를 생성하면 기본적으로는 현재 시점의 날짜의 시간 정보를 가진다
import Foundation
var now = Date()
print(now) // 2022-12-27 11:18:45 +0000
DateFormatter()
- DateFormatter를 이용해 date value 의 format을 설정할 수 있다.
예를 들어, 지역, 시간대를 설정하거나 날짜 출력 형식을 설정할 수 있다
date 시간대, 지역 설정
var locale: Locale!
// The locale for the receiver.
var timeZone: TimeZone!
// The time zone for the receiver.
import Foundation
var df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm"
print(df.string(from: Date())) // 2022-12-27 22:41
df.locale = Locale(identifier: "UTC")
df.timeZone = TimeZone(identifier: "UTC")
print(df.string(from: Date())) // 2022-12-27 13:41
date string 형식 설정
var dateFormat: String!
// The date format string used by the receiver.
var df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
print(df.string(from: Date())) // 2022-12-27
year | y / yy / yyyy | ex) 2022 y - 2022 / yy - 22 / yyyy - 2022 |
month | M / MM | ex) 7월 M - 7 / MM - 07 ex) 12월 M - 12 / MM - 12 |
MMM / MMMM | ex) 10월 Eng MMM - Oct / MMMM - October ex) 10월 Kor MMM - 10월 / MMMM - 10월 |
|
day | d / dd | ex) 9일 d - 9 / dd - 09 ex) 30일 d - 30 / dd - 30 |
day of week | E / EEEE | ex) 월 Eng E - Mon / EEEE - Monday ex) 월 Kor E - 월 / EEEE - 월요일 |
EEEEE / EEEEEE | ex) 월 EEEEE - M / EEEEEE - Mo |
|
hour | h / hh | ex) 2AM h - 2 / hh - 02 ex) 2PM h - 2 / hh - 02 |
H / HH | ex) 2AM H - 2 / HH - 02 ex) 2PM H - 14 / HH - 14 |
|
minute | m / mm | ex) 7분 m - 7 / mm - 07 |
second | s / ss | ex) 7초 s - 7 / ss - 07 |
milisecond | S / SS / SSS | ex) 0.321 S - 0.3 / SS - 0.32 / SSS - 0.321 |
AM / PM | a | ex) Eng AM / PM ex) Kor 오전 / 오후 |
Example Code
import Foundation
func set_dateFormat(strFormat : String, df1 : DateFormatter, df2 : DateFormatter) {
df1.dateFormat = strFormat
df2.dateFormat = strFormat
}
func print_date(d : Date, df1 : DateFormatter, df2 : DateFormatter) {
print(df1.string(from: d))
print(df2.string(from: d))
}
var dfKor = DateFormatter()
var dfEng = DateFormatter()
dfEng.locale = Locale(identifier: "UTC")
set_dateFormat(strFormat: "yyyy-MM-dd HH:mm", df1: dfKor, df2: dfEng)
let dateStr = "2022-03-21 16:30" // Date 형태의 String
var convertDate = dfKor.date(from: dateStr) // String 타입의 dateStr Date 타입으로 변환
print(dfKor.string(from: convertDate!)) // 2022-03-21 16:30
set_dateFormat(strFormat: "yy/M/d E", df1: dfKor, df2: dfEng)
print_date(d: convertDate!, df1: dfKor, df2: dfEng) // Kor: 22/3/21 월, Eng: 22/3/21 Mon
set_dateFormat(strFormat: "y.MM.dd E", df1: dfKor, df2: dfEng)
print_date(d: convertDate!, df1: dfKor, df2: dfEng) // Kor: 2022.03.21 월, Eng: 2022.03.21 Mon
set_dateFormat(strFormat: "yyyy MMM dd EEEE", df1: dfKor, df2: dfEng)
print_date(d: convertDate!, df1: dfKor, df2: dfEng) // Kor: 2022 3월 21 월요일, Eng: 2022 Mar 21 Monday
set_dateFormat(strFormat: "yyyy MMMM dd EEEE", df1: dfKor, df2: dfEng)
print_date(d: convertDate!, df1: dfKor, df2: dfEng) // Kor: 2022 3월 21 월요일, Eng: 2022 March 21 Monday
set_dateFormat(strFormat: "a hh:mm", df1: dfKor, df2: dfEng)
print_date(d: convertDate!, df1: dfKor, df2: dfEng) // Kor: 오후 04:30 ,Eng: PM 04:30
'iOS' 카테고리의 다른 글
[Swift] Ascii Code로 변환하기 (0) | 2022.12.28 |
---|