본문 바로가기

Project/30MinRead

🔥 시작일 ➡️ 종료일 포함하여 날짜 계산!

✅ 두 날짜 간의 차이를 일(day) 단위로 계산해주는 메서드

private func daysBetween(_ start: Date, _ end: Date) -> Int {
    let calendar = Calendar.current
    let components = calendar.dateComponents([.day], from: start, to: end)
    return (components.day ?? 0) + 2
}

 

 

✅ 메서드 사용하기

func configure(_ readItem: ReadItemModel) {
    titleLabel.text = readItem.title
    let startDate = readItem.startDate
    let endDate = readItem.endDate
    let today = Date()

    let startReadDate = self.formattedDate(startDate)
    let endReadDate = self.formattedDate(endDate)

    dateLabel.text = "📅 " + startReadDate + " ~ " + endReadDate

	// ✅ 두 날짜 간의 차이 계산
    let totalDays = self.daysBetween(startDate, endDate)
    let progressDays = self.daysBetween(startDate, today) - 2

    let validProgessDays = min(progressDays, totalDays)

    let completeArray = Array(repeating: "⚪️", count: totalDays)
    let updatedArray = completeArray.enumerated().map { index, circle in
        return index < validProgessDays ? "🟢" : circle
    }

    completeLabel.text = updatedArray.joined(separator: " ")
}