Frontend/Swift

ISOstring to date

괴발새발자 2023. 4. 18. 10:04

ISO 문자열을 date로 변환하기 : ISO8601DateFormatter를 이용하기
https://sarunw.com/posts/how-to-parse-iso8601-date-in-swift/

How to parse ISO 8601 date in Swift | Sarunw

Learn how to parse ISO 8601 date with ISO8601DateFormatter.

sarunw.com

ISO8601DateFormatter를 설정없이 사용시 nil 값을 반환한다. 
https://stackoverflow.com/questions/61341222/iso8601dateformatter-returning-nil-for-converting-from-valid-string-to-date

ISO8601DateFormatter returning nil for converting from valid string to date

I have an ISO8601 String that I want to convert to a Date object, yet I keep getting a nil even though it's a valid string. Here's my code: let isoFormatter = ISO8601DateFormatter()

stackoverflow.com

ISO date로부터 지금까지 얼마나 남았는 지 알아내는 커스텀 함수

func leftDayCalculator(_ isoString: String) -> Int {
    let dateFormatter = ISO8601DateFormatter()
    dateFormatter.formatOptions = [
        .withFractionalSeconds,
        .withFullDate,
        .withTime, // without time zone
        .withColonSeparatorInTime,
        .withDashSeparatorInDate
    ]
    let date = dateFormatter.date(from: isoString) ?? Date()
    let offsetComps = Calendar.current.dateComponents([.day], from: Date(), to: date)
    return offsetComps.day ?? 0
}

참조 자료 : https://gonslab.tistory.com/68

iOS Swift 날짜 차이 구하기 (요일, 시간 사이 간격 변환 계산) Calc Date

날짜 차이 구하기 let myDateComponents = DateComponents(year: 2022, month: 3, day: 01) let startDate = Calendar.current.date(from: myDateComponents)! let offsetComps = Calendar.current.dateComponents([.year,.month,.day], from: startDate, to: Date())

gonslab.tistory.com