两个时间间隔 ios 分量之间的差异 swift 3
difference between two time interval ios components swift 3
我有两个时间间隔,基本上我想转换找出它们之间的区别这是我的代码。但是,它不起作用
func dateformatter(date:Double) -> String {
let timeElapsed:TimeInterval = TimeInterval(Int(date))
let timeNow:TimeInterval = TimeInterval(integerLiteral: Int64(Date().timeIntervalSince1970*1000))
let date2:Date = Date(timeIntervalSince1970: timeElapsed )
let date1:Date = Date(timeIntervalSince1970: timeNow )
let calender:Calendar = Calendar.current
let components:DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute], from: date1, to: date2)
var returnString:String = ""
print(components.second)
if components.second! < 60 {
returnString = "Just Now"
}else if components.minute! >= 1{
returnString = String(describing: components.minute) + " min ago"
}else if components.hour! >= 1{
returnString = String(describing: components.hour) + " hour ago"
}else if components.day! >= 1{
returnString = String(describing: components.day) + " days ago"
}else if components.month! >= 1{
returnString = String(describing: components.month)+" month ago"
}else if components.year! >= 1 {
returnString = String(describing: components.year)+" year ago"
}
return returnString
}
不需要您在函数开头进行的所有转换。 Date(timeIntervalSince1970:)
接受 NSTimeInterval
,但是,对于 double
它只是一个 typealias
。
此外,因为您的第一个 if
语句检查 seconds
,您需要将 .seconds
包含到组件参数列表中。
您可以执行以下操作:
func dateformatter(date: Double) -> String {
let date1:Date = Date() // Same you did before with timeNow variable
let date2: Date = Date(timeIntervalSince1970: date)
let calender:Calendar = Calendar.current
let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2)
print(components)
var returnString:String = ""
print(components.second)
if components.second! < 60 {
returnString = "Just Now"
}else if components.minute! >= 1{
returnString = String(describing: components.minute) + " min ago"
}else if components.hour! >= 1{
returnString = String(describing: components.hour) + " hour ago"
}else if components.day! >= 1{
returnString = String(describing: components.day) + " days ago"
}else if components.month! >= 1{
returnString = String(describing: components.month)+" month ago"
}else if components.year! >= 1 {
returnString = String(describing: components.year)+" year ago"
}
return returnString
}
提示,您的逻辑中存在一个错误,导致函数 return "Just now" 在每种情况下都如此。所以这是修改后的代码:
func timeElapsed(date: Date) -> String {
let date1:Date = date
let date2: Date = Date() // Same you did before with timeNow variable
let calender:Calendar = Calendar.current
let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2)
print(components)
var returnString:String = ""
print(components.second!)
if components.second! < 60 {
returnString = "Just Now"
}
if components.minute! > 1{
returnString = String(describing: components.minute!) + " mins ago"
}
else if components.minute! == 1 {
returnString = "A minute ago"
}
if components.hour! > 1{
returnString = String(describing: components.hour!) + " hours ago"
}
else if components.hour == 1 {
returnString = "An hour ago"
}
if components.day! > 1{
returnString = String(describing: components.day!) + " days ago"
}
else if components.day! == 1 {
returnString = "Yesterday"
}
if components.month! > 1{
returnString = String(describing: components.month!)+" months ago"
}
else if components.month! == 1 {
returnString = "A month ago"
}
if components.year! > 1 {
returnString = String(describing: components.year!)+" years ago"
}
else if components.year! == 1 {
returnString = "A year ago"
}
return returnString
}
我有两个时间间隔,基本上我想转换找出它们之间的区别这是我的代码。但是,它不起作用
func dateformatter(date:Double) -> String {
let timeElapsed:TimeInterval = TimeInterval(Int(date))
let timeNow:TimeInterval = TimeInterval(integerLiteral: Int64(Date().timeIntervalSince1970*1000))
let date2:Date = Date(timeIntervalSince1970: timeElapsed )
let date1:Date = Date(timeIntervalSince1970: timeNow )
let calender:Calendar = Calendar.current
let components:DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute], from: date1, to: date2)
var returnString:String = ""
print(components.second)
if components.second! < 60 {
returnString = "Just Now"
}else if components.minute! >= 1{
returnString = String(describing: components.minute) + " min ago"
}else if components.hour! >= 1{
returnString = String(describing: components.hour) + " hour ago"
}else if components.day! >= 1{
returnString = String(describing: components.day) + " days ago"
}else if components.month! >= 1{
returnString = String(describing: components.month)+" month ago"
}else if components.year! >= 1 {
returnString = String(describing: components.year)+" year ago"
}
return returnString
}
不需要您在函数开头进行的所有转换。 Date(timeIntervalSince1970:)
接受 NSTimeInterval
,但是,对于 double
它只是一个 typealias
。
此外,因为您的第一个 if
语句检查 seconds
,您需要将 .seconds
包含到组件参数列表中。
您可以执行以下操作:
func dateformatter(date: Double) -> String {
let date1:Date = Date() // Same you did before with timeNow variable
let date2: Date = Date(timeIntervalSince1970: date)
let calender:Calendar = Calendar.current
let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2)
print(components)
var returnString:String = ""
print(components.second)
if components.second! < 60 {
returnString = "Just Now"
}else if components.minute! >= 1{
returnString = String(describing: components.minute) + " min ago"
}else if components.hour! >= 1{
returnString = String(describing: components.hour) + " hour ago"
}else if components.day! >= 1{
returnString = String(describing: components.day) + " days ago"
}else if components.month! >= 1{
returnString = String(describing: components.month)+" month ago"
}else if components.year! >= 1 {
returnString = String(describing: components.year)+" year ago"
}
return returnString
}
提示,您的逻辑中存在一个错误,导致函数 return "Just now" 在每种情况下都如此。所以这是修改后的代码:
func timeElapsed(date: Date) -> String {
let date1:Date = date
let date2: Date = Date() // Same you did before with timeNow variable
let calender:Calendar = Calendar.current
let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2)
print(components)
var returnString:String = ""
print(components.second!)
if components.second! < 60 {
returnString = "Just Now"
}
if components.minute! > 1{
returnString = String(describing: components.minute!) + " mins ago"
}
else if components.minute! == 1 {
returnString = "A minute ago"
}
if components.hour! > 1{
returnString = String(describing: components.hour!) + " hours ago"
}
else if components.hour == 1 {
returnString = "An hour ago"
}
if components.day! > 1{
returnString = String(describing: components.day!) + " days ago"
}
else if components.day! == 1 {
returnString = "Yesterday"
}
if components.month! > 1{
returnString = String(describing: components.month!)+" months ago"
}
else if components.month! == 1 {
returnString = "A month ago"
}
if components.year! > 1 {
returnString = String(describing: components.year!)+" years ago"
}
else if components.year! == 1 {
returnString = "A year ago"
}
return returnString
}