![](/img/trans.png)
[英]How can i compare two dates (NSDate) in Swift 3 and get the days between them?
[英]how can I calculate specific dates in Swift (using NSDate)
我正在swift
开发ios应用,并且我有一个带有7个选项的滑块。 每个选项代表特定的日期。
到目前为止,我的代码如下:
func convertValueToDate(value: Float) -> NSDate{
var currentDate:NSDate = NSDate()
switch(value){
case 1:
print("5 years ago")
return NSDate()
case 2:
print("one year ago")
return NSDate()
case 3:
print("six months ago")
return NSDate()
case 4:
print("one month ago")
return NSDate()
case 5:
print("one week ago")
return NSDate()
case 6:
print("yesterday")
return NSDate()
case 7:
print("today")
return NSDate()
default:
print("default date")
return NSDate()
}
}
如您在上方看到的-我在控制台中打印了要返回的内容。 但是我不想打印,而是以NSDate
格式返回这些日期。 我不知道如何计算每个选项的时间,因为我想每天有0:00:01 AM
。 因此,例如。 当用户输入数字7
我想将昨天0:00:01 AM
的确切日期返回给他。 当用户选择数字5
我想给他一个星期前的日期,时间为0:00:01 AM
,依此类推。 我该如何计算它,以便该函数始终向我返回0:00:01 AM
和计算出的日期?
您可以使用NSCalendar方法dateByAddingUnit:
func convertValueToDate(value: Float) -> NSDate {
struct Cal {
static let iso8601 = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
}
let now = NSDate()
print("now: ", now)
switch(value) {
case 1:
print("5 years ago")
return Cal.iso8601.dateByAddingUnit(.Year, value: -5, toDate: now, options: [])!
case 2:
print("one year ago")
return Cal.iso8601.dateByAddingUnit(.Year, value: -1, toDate: now, options: [])!
case 3:
print("six months ago")
return Cal.iso8601.dateByAddingUnit(.Month, value: -6, toDate: now, options: [])!
case 4:
print("one month ago")
return Cal.iso8601.dateByAddingUnit(.Month, value: -1, toDate: now, options: [])!
case 5:
print("one week ago")
return Cal.iso8601.dateByAddingUnit(.WeekOfYear, value: -1, toDate: now, options: [])!
case 6:
print("yesterday")
return Cal.iso8601.dateByAddingUnit(.Day, value: -1, toDate: now, options: [])!
case 7:
print("today")
return now
default:
print("default date")
return now
}
}
如果您需要返回当天的开始日期,则可以使用NSCalendar startOfDayForDate方法。
func convertValueToDate(value: Float) -> NSDate {
struct Cal {
static let iso8601 = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
}
let now = NSDate()
print("now: ", now)
let result: NSDate
switch(value) {
case 1:
print("5 years ago")
result = Cal.iso8601.dateByAddingUnit(.Year, value: -5, toDate: now, options: [])!
case 2:
print("one year ago")
result = Cal.iso8601.dateByAddingUnit(.Year, value: -1, toDate: now, options: [])!
case 3:
print("six months ago")
result = Cal.iso8601.dateByAddingUnit(.Month, value: -6, toDate: now, options: [])!
case 4:
print("one month ago")
result = Cal.iso8601.dateByAddingUnit(.Month, value: -1, toDate: now, options: [])!
case 5:
print("one week ago")
result = Cal.iso8601.dateByAddingUnit(.WeekOfYear, value: -1, toDate: now, options: [])!
case 6:
print("yesterday")
result = Cal.iso8601.dateByAddingUnit(.Day, value: -1, toDate: now, options: [])!
case 7:
print("today")
result = now
default:
print("default date")
result = now
}
return Cal.iso8601.startOfDayForDate(result)
}
这是Swift 3中的解决方案,因为它已经在这里(对于Swift 2.x,在类之前添加NS
前缀应该可以解决问题):
import Foundation
func convertValueToDate(value: Float) -> Date{
let calendar = Calendar.current
let currentDate = calendar.date(bySettingHour: 00, minute: 00, second: 01, of: Date())!
func dateByAdding(_ value: Int, _ component: Calendar.Component) -> Date {
return calendar.date(byAdding: component, value: value, to: currentDate)!
}
switch(value){
case 1:
print("5 years ago")
return dateByAdding(-5, .year)
case 2:
print("one year ago")
return dateByAdding(-1, .year)
case 3:
print("six months ago")
return dateByAdding(-6, .month)
case 4:
print("one month ago")
return dateByAdding(-1, .month)
case 5:
print("one week ago")
return dateByAdding(-7, .day)
case 6:
print("yesterday")
return dateByAdding(-1, .day)
case 7:
print("today")
return currentDate
default:
print("default date")
return currentDate
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.