mox*_*mlb 7 xcode nsdate date-comparison swift swift3
如何比较Swift 3中的两个日期?我找到了许多其他Swift版本的解决方案,但它们对我不起作用.
let clickedDay:String = currentcell.DayLabel.text!
let currentDate = NSDate()
let currentDay = "" //want the current day
let dateFormatter = DateFormatter()
var dateAsString = ""
if Int(clickedDay)! < 10 {
dateAsString = "0" + clickedDay + "-11-2016 GMT"
}
else {
dateAsString = clickedDay + "-11-2016 GMT"
}
dateFormatter.dateFormat = "dd-MM-yyyy zzz"
let clickedDate = dateFormatter.date(from: dateAsString)
if currentDate as Date >= clickedDate! as Date {
return true
}
else {
//Want to get the days between the currentDate and clickedDate
let daysLeft = ""
}
Run Code Online (Sandbox Code Playgroud)
我比较了拖曳日期,但我想要那些拖曳日期之间的日子.有人可以帮我吗?谢谢,问候
补充:如果我有日期22.11.2016和当前日期(现在是18.11.2016)我想要它之间的日子(在这种情况下是4)
Nir*_*v D 13
只需Date像这样进行扩展,并在几天内获得差异.
extension Date {
func daysBetweenDate(toDate: Date) -> Int {
let components = Calendar.current.dateComponents([.day], from: self, to: toDate)
return components.day ?? 0
}
}
Run Code Online (Sandbox Code Playgroud)
现在使用这个扩展名
let numOfDays = fromDate.daysBetweenDate(toDate: toDate)
Run Code Online (Sandbox Code Playgroud)
您可以使用DateFormatter像这样将String转换为Date.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
let date = dateFormatter.date(from: "18-11-2016")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9896 次 |
| 最近记录: |