迅速3如何获得明天和昨天的日期(请注意特殊情况)新月或新年 [英] swift 3 how to get date for tomorrow and yesterday ( take care special case ) new month or new year

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

问题描述

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

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

我是由于(特殊情况下的
的索引超出范围)而崩溃,例如今天的日期(2017年5月31日)
,如果我尝试继续运行我的应用程序并明天开始计算,则我有5月的数组遇到错误(因为我明天应该知道它的新月份)

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)

这是我的代码

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


推荐答案

您应使用日历方法 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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆