Post

Replies

Boosts

Views

Activity

Comment on Dateformatter with "dd-MMM-yyyy" for September month returning wrong date
I am just calculating age from string date which we receive from backend database /* Code which is not working for new version of iOS 15 */ let startDate = "01-Sep-1970" // got this date from backend let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MMM-yyyy" let formatedStartDate = dateFormatter.date(from: startDate) // getting nil here let currentDate = Date() let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .month, .year]) let differenceOfDate = Calendar.current.dateComponents(components, from: formatedStartDate!, to: currentDate) // getting crash here let age = Double(differenceOfDate.year ?? 0) /* Code which is working for older version of iOS 12 */ let startDate = "01-Sep-1970" // got this date from backend let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MMM-yyyy" let formatedStartDate = dateFormatter.date(from: startDate) let currentDate = Date() let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .month, .year]) let differenceOfDate = Calendar.current.dateComponents(components, from: formatedStartDate!, to: currentDate) let age = Double(differenceOfDate.year ?? 0) // getting expected value
Topic: App & System Services SubTopic: General Tags:
May ’22