I have a date format like this from the API result:
dd-MM-yyyy HH:mm:ss
What I want to do is to get the remaining days/hours/minutes from the date formatter. So my case are like this:
- If it's 7 days or less days remaining from the date, returns "x days remaining"
- If it's less than 24 hours remaining from the date, returns "x hours remaining"
- If it's less than an hour remaining from the date, returns "x minutes remaining"
- If else, returns "dd-MM-yyyy"
Here's my date formatter function:
private func convertDate(dateString: String) -> String {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "dd MMM yyyy"
if let date = dateFormatterGet.date(from: dateString) {
return dateFormatterPrint.string(from: date)
} else {
return " - "
}
}
I'm not sure how to achieve this because I'm still new to programming. Any help would be appreciated, thank you.