本文介绍了swift 3 如何获取明天和昨天的日期(注意特殊情况)新月或新年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理每个当月的数据日历我应该显示 3 种类型的计算(昨天 - 今天 - 明天)

I am working on data calendar for every current monthand I should show 3 type of calculation for ( yesterday - today - tomorrow)

由于特殊情况(索引超出范围)而导致崩溃就像今天日期(2017 年 5 月 31 日)如果我尝试继续我的应用程序并明天开始计算,我会有五月的数组,我将有错误(因为我明天应该知道它的新月份)

I am getting crash because of (index out of bound) for special caselike 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)

这是我的代码

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 {
                //////
            }
        }
    }
}

推荐答案

你应该使用 Calendar 方法 date(byAdding component:) 在中午时间做你的日历计算.这样做您无需担心那些特殊情况:

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 或更高版本

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

这篇关于swift 3 如何获取明天和昨天的日期(注意特殊情况)新月或新年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:40