Swift 将字符串转换为日期输出错误的日期
Swift convert string to date output wrong date
我想将 dateStartString = “28/02/2018”
转换为 Date
并将转换后的日期与今天的日期进行比较。当我转换 dateStartString
时,转换后的日期是 "2018-02-27 18:30:00 UTC"
。为什么它的输出是错误的日期?
这是我的代码
var dateStartString = "28/02/2018"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
guard let dateStartDate = dateFormatter.date(from: dateStartString) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}
let dateToday = Date()
if(dateStartDate>=dateToday){
print("Yes")
}
else{
print("Today date is 28/02/2018. Why it print No?")
}
希望你能理解我的问题。
提前致谢。
你要明白,Date
不仅仅代表一个日期,还代表一个时间。
>=
比较 Date
对象的日期和时间部分。由于您没有在日期字符串中指定任何时间,因此 API 假定它是您当地时间的 00:00:00,即 UTC 前一天的 18:30:00。为什么是 UTC,你问?这就是日期的 description
始终如此。当您打印日期时,它总是以 UTC 时间打印。要在您的时区打印它,请设置日期格式化程序的 timeZone
属性 并对其进行格式化。
仅比较日期部分的一种方法是删除时间部分。从此 ,这是删除时间分量的方法:
public func removeTimeStamp(fromDate: Date) -> Date {
guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: fromDate)) else {
fatalError("Failed to strip time from Date object")
}
return date
}
现在这应该是真的:
dateStartDate >= removeTimeStamp(fromDate: dateToday)
您需要 timeZone
为您的 dateFormatter
:
dateFormatter.timeZone = TimeZone(secondsFromGMT:0)!
因为 dateStartDate
位于 28/02/2018
的 00:00
,
而 dateToday
是当前时间点,即
在同一天,但 午夜之后。因此 dateStartDate >= dateToday
的计算结果为 false
.
仅将时间戳与天粒度进行比较并忽略
您可以使用的时间组件
if Calendar.current.compare(dateStartDate, to: dateToday, toGranularity: .day) != .orderedAscending {
print("Yes")
}
如果 dateStartDate
在相同或更晚的时间,这将打印 "Yes"
天比 dateToday
.
比较方法returns.orderedAscending
,.orderedSame
,
或 .orderedDescending
,取决于第一次约会是否在
比第二个日期早一天、同一天或晚一天。
尝试在比较日期时设置当前日期格式。
在您的示例代码更新下方:
var dateStartString = "28/02/2018"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.locale = NSLocale.current
guard let dateStartDate = dateFormatter.date(from: dateStartString) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}
var dateToday = Date()
print(dateToday)
let dateTodaystr = dateFormatter.string(from: dateToday)
dateToday = dateFormatter.date(from: dateTodaystr)!
print(dateToday)
if(dateStartDate>=dateToday){
print("Yes")
}
else{
print("Today date is 28/02/2018. Why it print No?")
}
我想将 dateStartString = “28/02/2018”
转换为 Date
并将转换后的日期与今天的日期进行比较。当我转换 dateStartString
时,转换后的日期是 "2018-02-27 18:30:00 UTC"
。为什么它的输出是错误的日期?
这是我的代码
var dateStartString = "28/02/2018"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
guard let dateStartDate = dateFormatter.date(from: dateStartString) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}
let dateToday = Date()
if(dateStartDate>=dateToday){
print("Yes")
}
else{
print("Today date is 28/02/2018. Why it print No?")
}
希望你能理解我的问题。 提前致谢。
你要明白,Date
不仅仅代表一个日期,还代表一个时间。
>=
比较 Date
对象的日期和时间部分。由于您没有在日期字符串中指定任何时间,因此 API 假定它是您当地时间的 00:00:00,即 UTC 前一天的 18:30:00。为什么是 UTC,你问?这就是日期的 description
始终如此。当您打印日期时,它总是以 UTC 时间打印。要在您的时区打印它,请设置日期格式化程序的 timeZone
属性 并对其进行格式化。
仅比较日期部分的一种方法是删除时间部分。从此
public func removeTimeStamp(fromDate: Date) -> Date {
guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: fromDate)) else {
fatalError("Failed to strip time from Date object")
}
return date
}
现在这应该是真的:
dateStartDate >= removeTimeStamp(fromDate: dateToday)
您需要 timeZone
为您的 dateFormatter
:
dateFormatter.timeZone = TimeZone(secondsFromGMT:0)!
因为 dateStartDate
位于 28/02/2018
的 00:00
,
而 dateToday
是当前时间点,即
在同一天,但 午夜之后。因此 dateStartDate >= dateToday
的计算结果为 false
.
仅将时间戳与天粒度进行比较并忽略 您可以使用的时间组件
if Calendar.current.compare(dateStartDate, to: dateToday, toGranularity: .day) != .orderedAscending {
print("Yes")
}
如果 dateStartDate
在相同或更晚的时间,这将打印 "Yes"
天比 dateToday
.
比较方法returns.orderedAscending
,.orderedSame
,
或 .orderedDescending
,取决于第一次约会是否在
比第二个日期早一天、同一天或晚一天。
尝试在比较日期时设置当前日期格式。 在您的示例代码更新下方:
var dateStartString = "28/02/2018"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.locale = NSLocale.current
guard let dateStartDate = dateFormatter.date(from: dateStartString) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}
var dateToday = Date()
print(dateToday)
let dateTodaystr = dateFormatter.string(from: dateToday)
dateToday = dateFormatter.date(from: dateTodaystr)!
print(dateToday)
if(dateStartDate>=dateToday){
print("Yes")
}
else{
print("Today date is 28/02/2018. Why it print No?")
}