๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming ๊ฐœ๋ฐœ/๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift

RelativeTimeFormatter๋กœ ์ƒ๋Œ€์‹œ๊ฐ„ ํ‘œ๊ธฐํ•˜๊ธฐ

by kimdee 2023. 3. 7.
๋ฐ˜์‘ํ˜•

 

 

 

 

์ง€๊ธˆ ํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ์—์„œ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ฌ ๋•Œ ISO8601 ํ˜•ํƒœ๋กœ ๋ฐ›์•„์˜ค๋Š”๋ฐ, ์ด๋ฅผ ํ˜„์žฌ ๋‚ ์งœ, ์‹œ๊ฐ„๊ณผ ๋น„๊ตํ•˜์—ฌ ์ƒ๋Œ€์‹œ๊ฐ„์œผ๋กœ ํ‘œ๊ธฐํ•ด์•ผ ํ–ˆ๋‹ค.

 

๋ณด์—ฌ์ฃผ๋Š” ํ˜•ํƒœ๋Š” ์•„๋ž˜ ํ˜•ํƒœ๋Œ€๋กœ ๋ณด์—ฌ์ฃผ๊ณ ์ž ํ–ˆ๋‹ค.

 

 ์ง€๊ธˆ, 5๋ถ„ ์ „, 3์‹œ๊ฐ„ ์ „, 2์ผ ์ „

 

Foundation ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ RelativeDateTimeFormatter ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ๋Œ€์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ์–ด์„œ ์ด๋ฅผ ์ด์šฉํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜์˜€๋‹ค.

์„œ๋ฒ„์—์„œ ๋ฌธ์ž์—ด๋กœ ๋ฐ›์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ Date ํฌ๋งท์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ String ์— extension ์œผ๋กœ ์„ ์–ธํ•ด์ฃผ๊ณ , ์ด๋ ‡๊ฒŒ ๋ฐ”๊พผ ๋‚ ์งœ๋ฅผ Date extension ์—์„œ ์ƒ๋Œ€์‹œ๊ฐ„์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค.

๋‚ ์งœ ๋ฌธ์ž์—ด์„ Date ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

// ๋ฌธ์ž์—ด -> ๋‚ ์งœ
extension String {
    func toDate() -> Date {
        let dateFormater = DateFormatter()
        dateFormater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
        guard let validDate = dateFormater.date(from: self) else { return Date() }
        
        return validDate
    }
}

 

ํ˜„์žฌ ๋‚ ์งœ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€์‹œ๊ฐ„ ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ

// ์ƒ๋Œ€์‹œ๊ฐ„ ๊ณ„์‚ฐํ•˜๊ธฐ
extension Date {
    /// ์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    /// - ์˜ˆ: 12์‹œ๊ฐ„ ์ „, 10๋ถ„ ์ „
    func toRelativeString() -> String {
        let formatter = RelativeDateTimeFormatter()
        formatter.locale = Locale(identifier: "ko_KR")
        formatter.dateTimeStyle = .named
        formatter.unitsStyle = .short
        let dateToString = formatter.localizedString(for: self, relativeTo: .now)
        return dateToString
    }
}

 

์‚ฌ์šฉ๋ฐฉ๋ฒ•

let writtenDate = "2023-03-05T15:08:43"
let relativeTimeFromNow = writtenDate.toString().toRelativeString()

 

์ฐธ๊ณ ๋งํฌ

RelativeDateTimeFormatter | Apple Developer Documentation

 

RelativeDateTimeFormatter | Apple Developer Documentation

A formatter that creates locale-aware string representations of a relative date or time.

developer.apple.com

How to show a relative date and time using RelativeDateTimeFormatter - free Swift 5.4 example code and tips

 

How to show a relative date and time using RelativeDateTimeFormatter - free Swift 5.4 example code and tips

Was this page useful? Let us know! 1 2 3 4 5

www.hackingwithswift.com

Relative Date Time Formatter / List Formatter ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

 

Relative Date Time Formatter / List Formatter ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š” :) Zedd์ž…๋‹ˆ๋‹ค.์ €๋ฒˆ๊ธ€ Ordered Collection Diffing๋ฅผ ์“ฐ๋‹ค๊ฐ€ Advances in Foundation๋ฅผ ๋‹ค์‹œ ๋ณด๊ฒŒ๋๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ์†Œ๊ฐœ๋œ ๊ธฐ๋Šฅ(?)์ด ์ •๋ง ๋งŽ์•„์š”! ์•„์ง ์•ˆ๋ณด์…จ๋‹ค๋ฉด ๊ผญ ๋ณด๋Š”๊ฑธ ์ถ”์ฒœ..!!!์•”ํŠผ ๊ทธ์ค‘ ํ•˜๋‚˜์ธ

zeddios.tistory.com

 


 

์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 

 

๊ถ๊ธˆํ•˜๊ฑฐ๋‚˜ ๋‚˜๋ˆ„๊ณ  ์‹ถ์€ ์–˜๊ธฐ๊ฐ€ ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”!

์žฌ๋ฐŒ๊ฒŒ ์ฝ์œผ์…จ๋‹ค๋ฉด ๊ณต๊ฐ๊ณผ ๊ตฌ๋…์€ ํฐ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค. 

 

ํ•ญ์ƒ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

์ด ๊ธ€์€ doy.oopy.io ์—๋„ ๋ฐœํ–‰๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€