How to make the st,nd,rd,th as supescript along with date and time in swift xcode 13.
I have set the items in the tableviewcell
From the tableView cell I’m setting the date and time like in below code
let date = meetingListArray[indexPath.row].callScheduleDate?.dateConversion(date: meetingListArray[indexPath.row].callScheduleDate ?? "") ?? ""
let toTime = meetingListArray[indexPath.row].timeTo?.timeConverter(date: meetingListArray[indexPath.row].timeTo ?? "") ?? ""
let fromTime = meetingListArray[indexPath.row].timeFrom?.timeConverter(date: meetingListArray[indexPath.row].timeFrom ?? "") ?? ""
let time = fromTime + " -" + toTime
cell.callDateAndTime.text = date + time
I have written the the dateFormatWithSuffix(), daySuffix() as extension
func dateFormatWithSuffix() -> String {
return "MMMM dd'(self.daySuffix())' yyyy | "
}
func daySuffix() -> String {
let calendar = Calendar.current
let components = (calendar as NSCalendar).components(.day, from: self)
let dayOfMonth = components.day
switch dayOfMonth {
case 1, 21, 31:
return "st"
case 2, 22:
return "nd"
case 3, 23:
return "rd"
default:
return "th"
}
}
2
Answers
You can Use the same code of @DonMag There is a small Correction.
Because we can't assign a NSAttributedString to a label.There will be an error if you use .text. "Cannot assign value of type 'NSMutableAttributedString' to type 'String?'".
One way to do this is with an Attributed string.
For example (using your
daySuffix()
func):Result: