![](/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.