Date < - > String ์ฌ์ด์ ๋ณํ์ ํํ ์ผ์ ๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ DateFormatter๋ฅผ ํตํด ๋ณํํฉ๋๋ค.
extension String {
func toDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let date = dateFormatter.date(from: self)
return date
}
}
extension Date {
func toString() -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let dateString = dateFormatter.string(from: self)
return dateString
}
}
๊ฐ๋จํ๊ฒ๋ ์์ฒ๋ผ ๋ณํํ ์ ์๊ณ , ๋ณดํต ์๋ฒ์ ISO 8601 ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ณํํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
'iOS ๐ฅ > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift์์ Abstract Class ๋ง๋ค๊ธฐ (0) | 2023.06.06 |
---|---|
Swift ์๋ฌ ์ฒ๋ฆฌ(Error Handling) (0) | 2021.06.25 |
Swift ํด๋์ค ์์๊ณผ ์ด๊ธฐํ (0) | 2021.06.25 |
Codable Protocol์ ์ด์ฉํด์ ๋ณต์กํ JSON Parsing ํ๊ธฐ (0) | 2021.04.15 |
Swift Enum๊ณผ Optional (0) | 2021.02.09 |