我想计算2个日期之间的时差,时差结果必须类似
7h 12m OR 4 d 5h 27m OR 1M 5d 3h 21m
我试过写一个这样的函数:
import UIKit
func calculateTimeDifference(from dateTime1: String, to dateTime2: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateAsString = dateTime1
let date1 = dateFormatter.date(from: dateAsString)!
let dateAsString2 = dateTime2
let date2 = dateFormatter.date(from: dateAsString2)!
let components : NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year]
let difference = (Calendar.current as NSCalendar).components(components, from: date1, to: date2, options: [])
var dateTimeDifferenceString = ""
if difference.day != 0 {
dateTimeDifferenceString = "\(difference.day!)d \(difference.hour!)h \(difference.minute!)m"
} else if difference.day == 0 {
dateTimeDifferenceString = "\(difference.hour!)h \(difference.minute!)m"
}
return dateTimeDifferenceString
}
但是当我实现这段代码时
let checkinTime = "2018-02-09 07:51:01"
let now = "2018-02-20 14:56:18"
calculateTimeDifference(from: checkinTime, to: now)
// Result: "4d 7h 5m"
我不知道为什么只有4天而不是11天
但是如果我在同一天实施它,它会给出正确的结果
let checkinTime = "2018-02-09 07:51:01"
let now = "2018-02-09 14:56:18"
calculateTimeDifference(from: checkinTime, to: now)
// "7h 5m"
这里出了什么问题?
答案 0 :(得分:2)
在第一种情况下,日期之间的差异是一周而且 四天,您将看到如果您打印计算的日期组件。
print(difference)
// year: 0 month: 0 day: 4 hour: 7 minute: 5 second: 17 weekOfMonth: 1 isLeapMonth: false
// ^^^^^^ ^^^^^^^^^^^^^^
这就是为什么你得到输出" 4d 7h 5m"而不是
预期" 11d 7h 5m"。要解决此问题,只需删除.weekOfMonth
来自日历单位:
let components : NSCalendar.Unit = [.second, .minute, .hour, .day, .month, .year]
此外,您可以直接使用Calendar
,而无需使用NSCalendar
桥接到let components: Set<Calendar.Component> = [.second, .minute, .hour, .day, .month, .year]
let difference = Calendar.current.dateComponents(components, from: date1, to: date2)
:
<base href="/">
<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<link href="~/Scripts/jquery-ui.css" rel="stylesheet" />
<script src="/Scripts/angular.js"></script>
<script src="/Scripts/angular-route.js"></script>
<script src="/Scripts/angular-ui-router.js"></script>
<script src="/Angular/Modules/MyApp.js"></script>
<script src="/Angular/Components/Products/Ctr_Products.js"></script>
<script src="/Angular/Factories/ProductFactory.js"></script>
<link href="/Content/bootstrap.css" rel="stylesheet" />
答案 1 :(得分:0)
请找到有效的解决方案:
func calculateTimeDifference(from dateTime1: String, to dateTime2: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateAsString = dateTime1
let date1 = dateFormatter.date(from: dateAsString)!
let dateAsString2 = dateTime2
let date2 = dateFormatter.date(from: dateAsString2)!
let components : NSCalendar.Unit = [.second, .minute, .hour, .day,.month, .year]
let difference = (Calendar.current as NSCalendar).components(components, from: date1, to: date2, options: [])
var dateTimeDifferenceString = ""
if difference.day != 0 {
dateTimeDifferenceString = "\(difference.day!)d \(difference.hour!)h \(difference.minute!)m"
} else if difference.day == 0 {
dateTimeDifferenceString = "\(difference.hour!)h \(difference.minute!)m"
}
return dateTimeDifferenceString
}
使用以下代码调用此函数:
让checkinTime =“2018-02-09 07:51:01”
现在=“2018-02-20 14:56:18”
self.calculateTimeDifference(from:checkinTime,to:now)
所以你会得到确切的差异日期。