DateType

Date

UTC (Coordinated Universal Time)

  • ๊ตญ์ œ์ ์ธ ํ‘œ์ค€ ์‹œ๊ฐ„ - ํ˜‘์ • ์„ธ๊ณ„์‹œ

    • ๊ธฐ์กด ํ‰๊ท  ํƒœ์–‘์‹œ์ธ ๊ทธ๋ฆฌ๋‹ˆ์น˜ ํ‘œ์ค€์‹œ(GMT)๋ฅผ ๋Œ€์ฒดํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

    • ์ผ๋ฐ˜์ ์œผ๋กœ GMT ์‹œ๊ฐ„๊ณผ UTC ์‹œ๊ฐ„์„ ํ˜ผ์šฉํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

    • ์˜๊ตญ, ๋Ÿฐ๋˜์˜ ๊ทธ๋ฆฌ๋‹ˆ์น˜ ์ฒœ๋ฌธ๋Œ€์˜ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ์‹œ๊ฐ„

  • ์˜ˆ์‹œ

    • ์šฐ๋ฆฌ๋‚˜๋ผ: UTC + 9 (์˜๊ตญ์„ ๊ธฐ์ค€์œผ๋กœ 9์‹œ๊ฐ„ ์•ž์„ ๋‹ค)

    • ๋‰ด์š• : UTC - 5 (5์‹œ๊ฐ„ ๋Š๋ฆฌ๋‹ค.)

    • ์˜ํ† ๊ฐ€ ๋„“์€ ๋‚˜๋ผ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ‘œ์ค€์‹œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (์ค‘๊ตญ์€ 1๊ฐœ)

      • ๋ฏธ๊ตญ(5), ๋Ÿฌ์‹œ์•„(11)

Date ๊ตฌ์กฐ์ฒด

๐Ÿ’ก Swift์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋‚ ์งœ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ตฌ์กฐ์ฒด์ด๋‹ค.

  • Date() : ํ˜„์žฌ ์‹œ์ ์˜ Date ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

    • ๊ธฐ์ค€ ์‹œ์  (reference data, 2001.01.01 00:00:00 UTC)์—์„œ ๋ช‡ ์ดˆ๊ฐ€ ๋–จ์–ด์ ธ์žˆ๋Š”์ง€๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

      • .timeIntervalSinceReferenceDate

      • TimeInterval Type, ์ดˆ ๋‹จ์œ„์˜ ๊ฐœ๋…์ด๋‹ค.

  • ์–‘๋ ฅ, ์Œ๋ ฅ ๋“ฑ ํƒ€์ž„ ์กด์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š” ๋…๋ฆฝ์ ์ธ ์‹œ๊ฐ„์˜ ๊ฐ’์ด๋‹ค.

    • ์•”์‹œ์ ์ธ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

    • ๋‹ฌ๋ ฅ, ํƒ€์ž„์กด (์ง€์—ญ)์„ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

    • ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ ์ ˆํ•œ ํ˜•์‹์˜ ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

let now - Date() // ์ƒ์„ฑ ์‹œ์ ์˜ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ (์ดˆ ๋‹จ์œ„)
print(now) // UTC ๊ธฐ์ค€์˜ ์‹œ๊ฐ„ ์ถœ๋ ฅ
now.timeIntervalSinceReferenceDate 

Calendar

Date์˜ ์‹ค์ œ ํ™œ์šฉ ๋ฐฉ๋ฒ•

  1. ์–‘๋ ฅ, ์Œ๋ ฅ์˜ ๋‹ฌ๋ ฅ์„ ๋‹ค๋ฃจ๋Š” Calendar ๊ตฌ์กฐ์ฒด ํ™œ์šฉ

  2. ์›ํ•˜๋Š” ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ˜•ํ•˜๊ธฐ ์œ„ํ•œ DateFormatter ํด๋ž˜์Šค ํ™œ์šฉ

Calendar๊ตฌ์กฐ์ฒด

  • ์ ˆ๋Œ€์‹œ์  Date๋ฅผ ์—ฐ๋Œ€/์—ฐ๋„/๋‚ ์งœ/์š”์ผ ๋“ฑ์˜ ๋‹ฌ๋ ฅ์˜ ์š”์†Œ๋กœ ๋ณ€ํ™˜์„ ๋„์™€์ค€๋‹ค.

  • Calendar.current ๊ทธ๋ ˆ๊ณ ๋ฆฌ๋ ฅ(Gregorian calendar: ์–‘๋ ฅ)

    • Calendar.autoupdatingCurrent ์œ ์ €๊ฐ€ ์„ ํƒํ•œ ๋‹ฌ๋ ฅ ๊ธฐ์ค€ (์„ธ๊ณ„์  ์„œ๋น„์Šค)

  • ์ง€์—ญ ์„ค์ •

    • ๋‚˜๋ผ๋งˆ๋‹ค ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ํ‘œ๊ธฐํ•˜๋Š” ํ˜•์‹๊ณผ ์–ธ์–ด๊ฐ€ ๋‹ค๋ฆ„

    var calendar = Calendar.current
    
    // ๋‹ฌ๋ ฅ์˜ ์ง€์—ญ, ๋‹ฌ๋ ฅ ํ‘œ๊ธฐ ๋ฐฉ๋ฒ•
    calendar.locale
    
    // timezone, UTC ์‹œ๊ฐ„๊ณผ ๊ด€๋ จ๋œ ๊ฐœ๋…
    calendar.timeZone
    
    calendar.locale = Locale(identifier: "ko_KR")

Date์˜ ๋…„/์›”/์ผ/์‹œ/๋ถ„/์ดˆ ํ™•์ธ

// 1) ๋‚ ์งœ - ๋…„ / ์›” / ์ผ
let year: Int = calendar.component(.year, from: now)
let month: Int = calendar.component(.month, from: now)
let day: Int = calendar.component(.day, from: now)

// 2) ์‹œ๊ฐ„ - ์‹œ / ๋ถ„ / ์ดˆ
let timeHour: Int = calendar.component(.hour, from: now)
let timeMinute: Int = calendar.component(.minute, from: now)
let timeSecond: Int = calendar.component(.second, from: now)

// ์š”์ผ
let weekday: Int = calendar.component(.weekday, from: now) // ์ผ: 1, ์›”: 2..

๐Ÿ’ก ์‹ค์ œ ์•ฑ์—์„œ ํ‘œ๊ธฐํ•  ๋•Œ, ์œ„์ฒ˜๋Ÿผ ๋ถ„๋ฆฌํ•˜์—ฌ Label์— ํ‘œ์‹œํ•œ๋‹ค.

let myCal = Calendar.current

var myDateCom = myCal.dateComponents([.year, .month, .day], from: now)
//myCal.dateComponents(<#T##components: Set<Calendar.Component>##Set<Calendar.Component>#>, from: <#T##Date#>)

myDateCom.year
myDateCom.month
myDateCom.day

print("\(myDateCom.year!)๋…„ \(myDateCom.month!)์›” \(myDateCom.day!)์ผ")

์˜ˆ์‹œ

  1. ๋‹ฌ๋ ฅ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜์ด ๊ณ„์‚ฐ

class Dog {
    var name: String
    var yearOfBirth: Int
    
    init(name: String, year: Int) {
        self.name = name
        self.yearOfBirth = year
    }
    
    // ๋‚˜์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ณ„์‚ฐ ์†์„ฑ
    var age: Int {
        get {
            let now = Date()
            let year = Calendar.current.component(.year, from: now)
            return year - yearOfBirth
        }
    }
}

let choco = Dog(name: "์ดˆ์ฝ”", year: 2015)
choco.age
  1. ์—ด๊ฑฐํ˜•์œผ๋กœ ์šฉ๋ฆฌ์„ ๋งŒ๋“ค๊ณ , ์˜ค๋Š˜์˜ ์š”์ผ ๊ณ„์‚ฐ

enum Weekday: Int {
    case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday
    
    static var today: Weekday {
        let weekday: Int = Calendar.current.component(.weekday, from: Date()) 
        return Weekday(rawValue: weekday)!
    }
}

// ์˜ค๋Š˜์ด ๋ฌด์Šจ์š”์ผ์ธ์ง€
let today = Weekday.today
  1. ๋‘ ๋‚ ์งœ ์‚ฌ์ด์˜ ์ผ ์ˆ˜ ๊ณ„์‚ฐ

let startDate = Date()
let endDate = startDate.addingTimeInterval(3600 * 24 * 60) // 60์ผ ํ›„

let calendar2 = Calendar.current
let someDays = calendar2.dateComponents([.day], from: startDate, to: endDate).day!

print("\(someDays)์ผ ํ›„")

DateFormatter

Date Formatter

๐Ÿ’ก ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ์›ํ•˜๋Š” ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค. RFC 3339 ํ‘œ์ค€์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ๋‹ค. Date๋ฅผ ํŠน์ • ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” **์ง€์—ญ, ์‹œ๊ฐ„๋Œ€, ๋‚ ์งœํ˜•์‹, ์‹œ๊ฐ„ํ˜•์‹**์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

let formatter = DateFormatter()

// 1. ์ง€์—ญ ์„ค์ •
formatter.locale = Locale(identifier: "ko_KR")

// 2. ์‹œ๊ฐ„๋Œ€ ์„ค์ •, default ๊ฐ’ ์กด์žฌ
formatter.timeZone = TimeZone.current

// 3. ๋‚ ์งœ ํ˜•์‹ ์„ค์ •
formatter.dateStyle = .full           // "Tuesday, April 13, 2021"
//formatter.dateStyle = .long         // "April 13, 2021"
//formatter.dateStyle = .medium       // "Apr 13, 2021"
//formatter.dateStyle = .none         // (๋‚ ์งœ ์—†์–ด์ง)
//formatter.dateStyle = .short        // "4/13/21"

// 4. ์‹œ๊ฐ„ ํ˜•์‹ ์„ ํƒ
formatter.timeStyle = .full           // "2:53:12 PM Korean Standard Time"
//formatter.timeStyle = .long         // "2:54:52 PM GMT+9"
//formatter.timeStyle = .medium       // "2:55:12 PM"
//formatter.timeStyle = .none         // (์‹œ๊ฐ„ ์—†์–ด์ง)
//formatter.timeStyle = .short        // "2:55 PM"

// ์‚ฌ์šฉ
let timeString = formatter.string(from: Date())
print(timeString)

์ปค์Šคํ…€ ํ˜•์‹์œผ๋กœ ์ƒ์„ฑ

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "ko_KR")
formatter.dateFormat = "yyyy๋…„ MMMM d์ผ(E)"

let timeString = formatter.string(from: Date())

๋ฌธ์ž์—ด์—์„œ Date๋กœ ๋ณ€ํ™˜

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd"

let date = newFormatter.date(from: "2023/01/01)
print(date) // Date ํ˜•์‹

๋‘ ๋‚ ์งœ ์‚ฌ์ด์˜ ์ฐจ์ด

let start = Date()
let end = start.addingTimeInterval(3600 * 24 * 60) // 60์ผ ํ›„
let formatter  DateFormatter()

formatter.locale = Locale(identifier: "ko_KR")
formatter.timeZone = TimeZone.current
// formatter.timeZone = TimeZone(identifier: "Asiz/Seoul")

formatter.dateStyle = .long
formatter.timeStyle = .none

print("๊ธฐ๊ฐ„: \(formatter.string(from: start)) - \(formatter.string(from: end))")

Date Component

DateComponents

  • ๋‚ ์งœ์™€ ์‹œ๊ฐ„์˜ ์š”์†Œ๋“ค์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ฒด

  • ์›ํ•˜๋Š” ํŠน์ • ๋‚ ์งœ, ์‹œ๊ฐ„์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

var components = DateComponents()
components.year = 2021
components.month = 1
components.day = 1

components.hour = 12
components.minute = 30
components.second = 0

let specifiedDate: Date = Calendar.current.date(fromt: components)!
// ๊ตฌ์กฐ์ฒด์˜ ํ™•์žฅ
extension Date {
    init?(y year: Int, m month: Int, d day: Int) {
        var components = DateComponents()
        components.year = year
        components.month = month
        componetns.day = day

    guard let date = Calendar.curretn.date(from: components) else {
        return nil // ์‹คํŒจ๊ฐ€๋Šฅ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 
    } 

    self = date // ๊ตฌ์กฐ์ฒด์ด๊ธฐ๋•Œ๋ฌธ์— self์— ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ํ• ๋‹น ๋ฐฉ์‹์œผ๋กœ ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
                // ํž™์— ๋งŒ๋“ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅ self vs Self ๋น„๊ต
    }
}

datecomponents

  • ์ ˆ๋Œ€์  ์‹œ์ (์ดˆ๊ธฐ์ค€)์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  • ์›ํ•˜๋Š” ๋‚ ์งœ, ์‹œ๊ฐ„์œผ๋กœ ์„ธํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ง€์†์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌํ˜„

  • ๊ณ„์† ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ์ฝ”๋“œ๋Š” ์„ฑ๊ฒฉ์„ ํŒŒ์•…ํ•ด์„œ ์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ ๋“ฑ์œผ๋กœ ๊ตฌํ˜„ํ•ด์„œ ์ฝ”๋“œ์— ๋„ฃ๋Š”๋‹ค.

  • ์‹ค์ œ๋กœ๋Š” ๊ณ„์‚ฐ ์†์„ฑ(๋ณดํŽธ์ ), ํƒ€์ž… ๋ฉ”์„œ๋“œ, ์…์„ฑ์ž, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ๋“ฑ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

Last updated