英文:
Comparing date string from today's date in swift
问题
我正在尝试比较一个以字符串表示的给定日期与今天的日期。
func getDateFromDateString(date: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // 设置地区为可靠的US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: date) ?? Date()
return date
}
let ticketDate = getDateFromDateString(date: "2023-04-04")
let currentDate = Date()
if currentDate > ticketDate {
print("已过期")
} else {
print("活跃")
}
结果应该是 "活跃",但它显示为 "已过期",我不知道为什么。
英文:
I am trying to compare a given date which is in string with today's date
func getDateFromDateString(date: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd"//"yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from:date) ?? Date()
return date
}
let ticketDate = getDateFromDateString(date: "2023-04-04")
let currentDate = Date()
if currentDate > ticketDate {
print("Expired")
} else {
print("Active")
}
The result should be Active but it's Expired I don't know why
答案1
得分: 1
用以下代码替换这行:
if Calendar.current.compare(currentDate,
to: ticketDate,
toGranularity: .day) == .orderedAscending {
英文:
Replace the line
if currentDate > ticketDate {
with
if Calendar.current.compare(currentDate,
to: ticketDate,
toGranularity: .day) == .orderedAscending {
it compares the dates ignoring less significant date components than day
答案2
得分: 0
替换代码如下:
func getDateFromDateString(dateString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)
return date
}
func todayDate() -> Date {
let currentDate = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: currentDate)
let month = calendar.component(.month, from: currentDate)
let day = calendar.component(.day, from: currentDate)
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
let date = calendar.date(from: dateComponents)!
return date
}
let currentDate = todayDate()
let ticketDate = getDateFromDateString(dateString: "2023-04-04")!
if ticketDate >= currentDate {
print("Active")
} else {
print("Expired")
}
在你的代码中,当前日期包括时间信息,所以我已经从今天的日期中删除了时间,以便与日期进行比较,如果找到大于当前时间的日期,将打印为已过期。如果需要更详细的解决方案,请使用包括时间的完整日期。如果您想了解更多,请留下评论,我很乐意解释。
英文:
Replaced code by these
func getDateFromDateString(dateString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: dateString)
return date
}
func todayDate() -> Date{
let currentDate = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: currentDate)
let month = calendar.component(.month, from: currentDate)
let day = calendar.component(.day, from: currentDate)
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
let date = calendar.date(from: dateComponents)!
return date
}
let currentDate = todayDate()
let ticketDate = getDateFromDateString(dateString: "2023-04-04")!
if ticketDate >= currentDate {
print("Active")
} else {
print("Expired")
}
On your code current date have time as well so please remove time from today date. So it's compare with time which it will found greater and print as Expired. For better solution please use full date i.e. with time. If you want to know more please comment. I am happy to explain.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论