gpt4 book ai didi

swift - 比较今天和过去的日期

转载 作者:行者123 更新时间:2023-11-28 12:25:02 25 4
gpt4 key购买 nike

我是 Swift 的新手。我对日期有疑问。

如果我得到一个像 '2016-01-25T10:04:53.498Z' 这样的日期字符串,但我想比较现在的日期和日期字符串。我该怎么办?

我以TSWeChat为引用。

let currentDate = NSDate() 

func week() -> String {
let myWeekday: Int = (Calendar.current as NSCalendar).components([NSCalendar.Unit.weekday], from: self).weekday!
switch myWeekday {
case 0:
return "Sun"
case 1:
return "Mon"
case 2:
return "Tue"
case 3:
return "Wed"
case 4:
return "Thu"
case 5:
return "Fri"
case 6:
return "Sat"
default:
break
}
return "none"
}

var chatTimeString: String {
get {
let calendar = Calendar.current
let now = Date()
let unit: NSCalendar.Unit = [
NSCalendar.Unit.minute,
NSCalendar.Unit.hour,
NSCalendar.Unit.day,
NSCalendar.Unit.month,
NSCalendar.Unit.year,
]
let nowComponents:DateComponents = (calendar as NSCalendar).components(unit, from: now)
let targetComponents:DateComponents = (calendar as NSCalendar).components(unit, from: self)

let year = nowComponents.year! - targetComponents.year!
let month = nowComponents.month! - targetComponents.month!
let day = nowComponents.day! - targetComponents.day!

if year != 0 {
return String(format: "%zdY%zdM%zdD %02d:%02d", targetComponents.year!, targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
} else {
if (month > 0 || day > 7) {
return String(format: "%zdM%zdD %02d:%02d", targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
} else if (day > 2) {
return String(format: "%@ %02d:%02d",self.week(), targetComponents.hour!, targetComponents.minute!)
} else if (day == 2) {
if targetComponents.hour! < 12 {
return String(format: "The day before yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else if targetComponents.hour == 12 {
return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else {
return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
}
} else if (day == 1) {
if targetComponents.hour! < 12 {
return String(format: "Yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else if targetComponents.hour == 12 {
return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else {
return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
}
} else if (day == 0){
if targetComponents.hour! < 12 {
return String(format: "A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else if targetComponents.hour == 12 {
return String(format: "P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
} else {
return String(format: "P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
}
} else {
return ""
}
}
}
}

最佳答案

[基于这个问题的标题。] 不要比较日期组件。直接比较日期。如果您有日期字符串,请使用 DateFormatter 将其转换回日期。

  2> var past = Date ()
past: Date = 2017-04-28 03:59:08 UTC
3> var now = Date()
now: Date = 2017-04-28 03:59:15 UTC
4> now > past
$R0: Bool = true

关于swift - 比较今天和过去的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com