【发布时间】:2020-12-04 18:34:44
【问题描述】:
enter image description here我有两个日期格式的日期。我使用以下代码来查找两个日期之间的差异。
extension Date {
/// Returns the amount of years from another date
func years(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}
/// Returns the amount of months from another date
func months(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}
/// Returns the amount of weeks from another date
func weeks(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
}
/// Returns the amount of days from another date
func days(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
/// Returns the amount of hours from another date
func hours(from date: Date) -> Int {
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}
/// Returns the amount of minutes from another date
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
/// Returns the amount of seconds from another date
func seconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
/// Returns the a custom time interval description from another date
func offset(from date: Date) -> String {
if years(from: date) > 0 { return "\(years(from: date))y" }
if months(from: date) > 0 { return "\(months(from: date))M" }
if weeks(from: date) > 0 { return "\(weeks(from: date))w" }
if days(from: date) > 0 { return "\(days(from: date))d" }
if hours(from: date) > 0 { return "\(hours(from: date))h" }
if minutes(from: date) > 0 { return "\(minutes(from: date))m" }
if seconds(from: date) > 0 { return "\(seconds(from: date))s" }
return ""
}
func offsetLong(from date: Date) -> String {
if years(from: date) > 0 { return years(from: date) > 1 ? "\(years(from: date)) yrs ago" : "\(years(from: date)) yr ago" }
if months(from: date) > 0 { return months(from: date) > 1 ? "\(months(from: date)) mo ago" : "\(months(from: date)) mo ago" }
if weeks(from: date) > 0 { return weeks(from: date) > 1 ? "\(weeks(from: date)) wks ago" : "\(weeks(from: date)) wk ago" }
if days(from: date) > 0 { return days(from: date) > 1 ? "\(days(from: date)) days ago" : "\(days(from: date)) day ago" }
if hours(from: date) > 0 { return hours(from: date) > 1 ? "\(hours(from: date)) hrs ago" : "\(hours(from: date)) hr ago" }
if minutes(from: date) > 0 { return minutes(from: date) > 1 ? "\(minutes(from: date)) mins ago" : "\(minutes(from: date)) min ago" }
if seconds(from: date) > 0 { return seconds(from: date) > 1 ? "\(seconds(from: date)) secs ago" : "\(seconds(from: date)) sec ago" }
return ""
}
}
我已在 EDT 时区的两个日期应用此扩展,如下所示:
let dateFormatterGet = DateFormatter()
// dateFormatterGet.timeZone = TimeZone(abbreviation: "UTC")
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
// dateFormatterPrint.timeZone = TimeZone(abbreviation: "UTC")
dateFormatterPrint.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date1: Date? = dateFormatterGet.date(from: date) as Date?
// print("date1 is",date1)
let now = Date()
let dateFormatterGet1 = DateFormatter()
dateFormatterGet1.timeZone = TimeZone(abbreviation: "EDT")
dateFormatterGet1.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint1 = DateFormatter()
dateFormatterPrint1.timeZone = TimeZone(abbreviation: "EDT")
dateFormatterPrint1.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dat = dateFormatterPrint1.string(from: now) as String
let date2: Date? = dateFormatterGet1.date(from: dat) as Date?
let timeOffset3 = date2!.offsetLong(from: date1!)
cell2.dateposted.text = timeOffset3
时间偏移值一直显示到几小时,而不是以分钟或秒显示。 谁能告诉我这个转换有什么问题?
【问题讨论】:
-
有一个内置类 DateComponentsFormatter 完全符合您的要求。你为什么不用那个?
-
如果您可以 a) 重新格式化您的第二段代码以避免它在每行的开头有大量(不一致的)空格,那将非常有帮助,因此我们必须滚动到结尾, 和 b) 显示您正在使用的值、您希望看到的内容以及您实际看到的内容。
-
@JonSkeet 你能告诉我在哪里吗?
-
@DuncanC 有什么参考链接吗?
-
在 Xcode 帮助系统中查找 DateComponentsFormatter。
标签: swift date timezone dateformatter