-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Add, Feat] #6 - date 생성, str 변환 함수 생성
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
35-seminar/Presentation/Week2/Extensions/Date+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)년 전" | ||
} | ||
} |