Skip to content

Commit

Permalink
[Add, Feat] #6 - date 생성, str 변환 함수 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
yurim830 committed Oct 25, 2024
1 parent 14a08bf commit f0d299c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 35-seminar/Presentation/Week2/Extensions/Date+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Date+Extension.swift
// 35-seminar
//
// Created by 김유림 on 10/25/24.
//

import Foundation

extension Date {
static func form(year: Int, month: Int, day: Int) -> Date? {
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day

return Calendar.current.date(from: dateComponents)
}

static func formattedDate(date: Date?) -> String {
guard let inputDate = date else { return "날짜 없음" }

let currentYear = Calendar.current.component(.year, from: Date())
let inputYear = Calendar.current.component(.year, from: inputDate)
let yearDifference = currentYear - inputYear

guard yearDifference > 0 else {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "M월 d"
return dateFormatter.string(from: inputDate)
}

return "\(yearDifference)년 전"
}
}

0 comments on commit f0d299c

Please sign in to comment.