ios swift 3 如何获得明天和昨天的日期(注意特殊情况)新的月份或新年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44009804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift 3 how to get date for tomorrow and yesterday ( take care special case ) new month or new year
提问by joshua
I am working on data calendar for every current month and I should show 3 type of calculation for ( yesterday - today - tomorrow)
我正在为每个当月的数据日历工作,我应该显示 3 种类型的计算(昨天 - 今天 - 明天)
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)
我因为(索引越界)而崩溃,就像今天日期(2017 年 5 月 31 日)这样的特殊情况,如果我尝试继续我的应用程序并明天开始计算,我有 5 月份的数组,我将有错误(因为我应该知道明天是新的月份)
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 {
//////
}
}
}
}
回答by Leo Dabus
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:
您应该使用 Calendar 方法date(byAdding component:)
使用中午时间进行日历计算。这样做您无需担心这些特殊情况:
Swift 3 or Later
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
回答by Vader
I don't know if this helps you or not, but I would recommend not doing the math on NSDateComponents
but rather on NSDate. Take a look at https://github.com/malcommac/SwiftDatea cocoapod for how to do things like call tomorrow
on an NSDate. The cool thing about that pod is it uses Regions
which helps for internationalization where the next day might start at sunset instead of mid nite.
我不知道这是否对您有帮助,但我建议不要NSDateComponents
在 NSDate上进行数学计算。看看https://github.com/malcommac/SwiftDatea cocoapod 如何做一些事情,比如tomorrow
在 NSDate 上调用。这个吊舱很酷的地方在于Regions
它有助于国际化,第二天可能从日落而不是午夜开始。