使用 swift 3 和 xcode 8 的 2 个日期(以周和天为单位)之间的差异
Difference between 2 dates in weeks and days using swift 3 and xcode 8
我正在尝试在几周和几天内计算 2 个日期(一个是当前日期,另一个来自日期选择器)之间的差异,然后在标签上显示结果,这就是我到目前为止所做的,我很感激在这里获得更有经验的开发人员的帮助!
let EDD = datePicker.date
let now = NSDate()
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .short
formatter.allowedUnits = [.day]
formatter.maximumUnitCount = 2
let string = formatter.string (from: now as Date, to: EDD)
label.text = string
您可以使用 Calendar
的 dateComponents(_:from:to:)
来计算 2 个日期与所需单位之间的差异。
示例:
let dateRangeStart = Date()
let dateRangeEnd = Date().addingTimeInterval(12345678)
let components = Calendar.current.dateComponents([.weekOfYear, .month], from: dateRangeStart, to: dateRangeEnd)
print(dateRangeStart)
print(dateRangeEnd)
print("difference is \(components.month ?? 0) months and \(components.weekOfYear ?? 0) weeks")
> 2017-02-17 10:05:19 +0000
> 2017-07-10 07:26:37 +0000
> difference is 4 months and 3 weeks
let months = components.month ?? 0
let weeks = components.weekOfYear ?? 0
你很接近。只需添加 .weekOfMonth
(意思是 "quantity of weeks"
根据 API 文档)到允许的单位。
示例:
let now = Date()
let endDate = now.addingTimeInterval(24 * 3600 * 17)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .weekOfMonth]
formatter.unitsStyle = .full
let string = formatter.string(from: now, to: endDate)!
print(string) // 2 weeks, 3 days
不需要设置maximumUnitCount = 2
因为有
只有两个允许的单位。
Swift 4
使用 Xcode 版本 9.2
进行测试
combining the above two answers that helps me
使用以下方法通过两个日期之间的差异获取字符串,其想法是通过格式本地化单词周/天/分钟。
func getTimeComponentString(olderDate older: Date,newerDate newer: Date) -> (String?) {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
let componentsLeftTime = Calendar.current.dateComponents([.minute , .hour , .day,.month, .weekOfMonth,.year], from: older, to: newer)
let year = componentsLeftTime.year ?? 0
if year > 0 {
formatter.allowedUnits = [.year]
return formatter.string(from: older, to: newer)
}
let month = componentsLeftTime.month ?? 0
if month > 0 {
formatter.allowedUnits = [.month]
return formatter.string(from: older, to: newer)
}
let weekOfMonth = componentsLeftTime.weekOfMonth ?? 0
if weekOfMonth > 0 {
formatter.allowedUnits = [.weekOfMonth]
return formatter.string(from: older, to: newer)
}
let day = componentsLeftTime.day ?? 0
if day > 0 {
formatter.allowedUnits = [.day]
return formatter.string(from: older, to: newer)
}
let hour = componentsLeftTime.hour ?? 0
if hour > 0 {
formatter.allowedUnits = [.hour]
return formatter.string(from: older, to: newer)
}
let minute = componentsLeftTime.minute ?? 0
if minute > 0 {
formatter.allowedUnits = [.minute]
return formatter.string(from: older, to: newer) ?? ""
}
return nil
}
这是您可以使用的方法
let nowDate = Date()
let endDate = Date().addingTimeInterval(12345678)
let string = String.getTimeComponentString(olderDate: nowDate, newerDate: endDate)!
print(nowDate)
print(endDate)
print(string)
Output
- 2018-03-0808:11:22+0000
- 2018-07-29 05:32:41 +0000
- 4个月
Swift 4,5
你可以用这一行代码找到以周为单位的不同日期。
let totalWeeks = Calendar.current.dateComponents([.weekOfMonth], from: Date(), to: endDate).weekOfMonth!
//for totalDays
let totalDays = Calendar.current.dateComponents([.day], from: Date(), to: endDate).day!
我正在尝试在几周和几天内计算 2 个日期(一个是当前日期,另一个来自日期选择器)之间的差异,然后在标签上显示结果,这就是我到目前为止所做的,我很感激在这里获得更有经验的开发人员的帮助!
let EDD = datePicker.date
let now = NSDate()
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .short
formatter.allowedUnits = [.day]
formatter.maximumUnitCount = 2
let string = formatter.string (from: now as Date, to: EDD)
label.text = string
您可以使用 Calendar
的 dateComponents(_:from:to:)
来计算 2 个日期与所需单位之间的差异。
示例:
let dateRangeStart = Date()
let dateRangeEnd = Date().addingTimeInterval(12345678)
let components = Calendar.current.dateComponents([.weekOfYear, .month], from: dateRangeStart, to: dateRangeEnd)
print(dateRangeStart)
print(dateRangeEnd)
print("difference is \(components.month ?? 0) months and \(components.weekOfYear ?? 0) weeks")
> 2017-02-17 10:05:19 +0000
> 2017-07-10 07:26:37 +0000
> difference is 4 months and 3 weeks
let months = components.month ?? 0
let weeks = components.weekOfYear ?? 0
你很接近。只需添加 .weekOfMonth
(意思是 "quantity of weeks"
根据 API 文档)到允许的单位。
示例:
let now = Date()
let endDate = now.addingTimeInterval(24 * 3600 * 17)
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .weekOfMonth]
formatter.unitsStyle = .full
let string = formatter.string(from: now, to: endDate)!
print(string) // 2 weeks, 3 days
不需要设置maximumUnitCount = 2
因为有
只有两个允许的单位。
Swift 4
使用 Xcode 版本 9.2
进行测试combining the above two answers that helps me
使用以下方法通过两个日期之间的差异获取字符串,其想法是通过格式本地化单词周/天/分钟。
func getTimeComponentString(olderDate older: Date,newerDate newer: Date) -> (String?) {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
let componentsLeftTime = Calendar.current.dateComponents([.minute , .hour , .day,.month, .weekOfMonth,.year], from: older, to: newer)
let year = componentsLeftTime.year ?? 0
if year > 0 {
formatter.allowedUnits = [.year]
return formatter.string(from: older, to: newer)
}
let month = componentsLeftTime.month ?? 0
if month > 0 {
formatter.allowedUnits = [.month]
return formatter.string(from: older, to: newer)
}
let weekOfMonth = componentsLeftTime.weekOfMonth ?? 0
if weekOfMonth > 0 {
formatter.allowedUnits = [.weekOfMonth]
return formatter.string(from: older, to: newer)
}
let day = componentsLeftTime.day ?? 0
if day > 0 {
formatter.allowedUnits = [.day]
return formatter.string(from: older, to: newer)
}
let hour = componentsLeftTime.hour ?? 0
if hour > 0 {
formatter.allowedUnits = [.hour]
return formatter.string(from: older, to: newer)
}
let minute = componentsLeftTime.minute ?? 0
if minute > 0 {
formatter.allowedUnits = [.minute]
return formatter.string(from: older, to: newer) ?? ""
}
return nil
}
这是您可以使用的方法
let nowDate = Date()
let endDate = Date().addingTimeInterval(12345678)
let string = String.getTimeComponentString(olderDate: nowDate, newerDate: endDate)!
print(nowDate)
print(endDate)
print(string)
Output
- 2018-03-0808:11:22+0000
- 2018-07-29 05:32:41 +0000
- 4个月
Swift 4,5
你可以用这一行代码找到以周为单位的不同日期。
let totalWeeks = Calendar.current.dateComponents([.weekOfMonth], from: Date(), to: endDate).weekOfMonth!
//for totalDays
let totalDays = Calendar.current.dateComponents([.day], from: Date(), to: endDate).day!