I am working on data calendar for every current month and I should show 3 type of calculation for ( yesterday - today - tomorrow)
I am getting crash because of (index out of bound) for special case like if today date ( 31 May 2017 ) and I have array of May month if I try to continue my app and start calculation tomorrow I will have Error (because I should know its new month tomorrow)
This is my code
class ViewController: UIViewController {
var dateComponents: DateComponents!
var TimeToday :[String] = []
var TimeTomorrow :[String] = []
var TimeYesterday :[String] = []
override func viewDidLoad() {
super.viewDidLoad()
let dateDay = Date()
let calendar = Calendar(identifier: .gregorian)
dateComponents = calendar.dateComponents([.day, .month, .year], from: dateDay)
done()
}
func done() {
//------ for tomorrow ------
// month end day 31
if (dateComponents.day! == 30){
if(dateComponents.month! == 1 || dateComponents.month! == 4 || dateComponents.month! == 6 || dateComponents.month! == 7 || dateComponents.month! == 9 || dateComponents.month! == 11 ){
TimeTomorrow.append("\(dateComponents.month!+1)")
}
// month end day 31
}else if (dateComponents.day! == 31){
if(dateComponents.month! == 3 || dateComponents.month! == 5 || dateComponents.month! == 8 ){
TimeTomorrow.append("\(dateComponents.month!+1)")
}else if(dateComponents.month! == 12){
TimeTomorrow.append("\(dateComponents.year!+1)")
}
// month end day 29
// special case for leap year i donot know how find it
//****************************************************
else if (dateComponents.day! == 29){
if(dateComponents.month! == 2 || dateComponents.month! == 10 ){
TimeTomorrow.append("\(dateComponents.month!+1)")
}
}
//------ for yesterday ------
if (dateComponents.month! == 12 || dateComponents.month! == 10 || dateComponents.month! == 8 || dateComponents.month! == 7 || dateComponents.month! == 5 || dateComponents.month! == 2){
var day = dateComponents.date! - 1
//fatal error: unexpectedly found nil while unwrapping an Optional value
TimeYesterday.append("\(day)")
TimeYesterday.append("30 - \(dateComponents.month! - 1)")
}else {
//////
}
}
}
}
You should use Calendar method date(byAdding component:)
to do your calendrical calculations using noon time. Doing so you don't need to worry about those special cases:
Swift 3 or Later
extension Date {
static var yesterday: Date { return Date().dayBefore }
static var tomorrow: Date { return Date().dayAfter }
var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: noon)!
}
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var month: Int {
return Calendar.current.component(.month, from: self)
}
var isLastDayOfMonth: Bool {
return dayAfter.month != month
}
}
Date.yesterday // "Oct 28, 2018 at 12:00 PM"
Date() // "Oct 29, 2018 at 11:01 AM"
Date.tomorrow // "Oct 30, 2018 at 12:00 PM"
Date.tomorrow.month // 10
Date().isLastDayOfMonth // false