Ios 字符串中的日期未给出正确的日期
我想将EST设置为每个用户的默认时区,但有一个条件需要在当前日期下午7:45检查。所以我比较了两个日期,但问题是当我将当前日期转换为字符串时,它给了我正确的EST时间,当我将该字符串再次转换为EST中的日期时,它给了我比EST提前4小时的时间。这是转换的代码Ios 字符串中的日期未给出正确的日期,ios,string,date,swift4,nsdateformatter,Ios,String,Date,Swift4,Nsdateformatter,我想将EST设置为每个用户的默认时区,但有一个条件需要在当前日期下午7:45检查。所以我比较了两个日期,但问题是当我将当前日期转换为字符串时,它给了我正确的EST时间,当我将该字符串再次转换为EST中的日期时,它给了我比EST提前4小时的时间。这是转换的代码 class func getCurrentDateTime() -> String { let dateFormatter = DateFormatter() dateFormatter.timeZone = Time
class func getCurrentDateTime() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString = dateFormatter.string(from: Date())
print(dateString)
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDate = convertDateFormatter.string(from: Date())
print(currentDate)
let comparedDateFormatter = DateFormatter()
comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")
print(comparedDate)
let currentDateFormatter = DateFormatter()
currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
comparedDateFormatter.timeZone = TimeZone.init(abbreviation: "EST")
let currentDateAndTime = currentDateFormatter.date(from: dateString)
print(currentDateAndTime)
return dateString
}
请检查此代码。以便日期没有时区。 因此,当我使用dateFormatter将日期转换为字符串时,字符串的表示形式将反映dateFormatter设置的时区。 但是,当您使用相同的格式化程序将字符串转换回日期时,该日期将不再具有时区偏移量。 所以我觉得这好像工作正常 编辑: 因此,如果您试图比较两个日期,我会做如下操作:
let date1 = Date()
let date2 = Date().addingTimeInterval(100)
if date1 == date2 {
// dates are exactly equal
} else if date1 > date2 {
// date1 is the most recent
} else if date1 < date2 {
// date2 is the most recent
}
如果我试图显示这些日期,我会使用日期格式化程序将它们转换为字符串。事实上,我没有发现代码有任何错误 日期始终在UTC时区中。日期格式化程序发挥了神奇的作用 要按照以下格式打印日期: 使用stringfrom:method 注意:当使用固定格式的日期时,例如RFC3339,您可以设置 用于指定格式字符串的dateFormat属性。对于大多数固定的 格式,还应将locale属性设置为POSIX语言环境 en_US_POSIX,并将时区属性设置为UTC
我按照您的要求修改了代码:
func getCurrentDateTime() -> String {
var checkString : String!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss a"
let dateString:String! = dateFormatter.string(from: Date())
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone(abbreviation: "EST")
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString1:String! = dateFormatter1.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDateformat = convertDateFormatter.string(from: Date())
let compareDate = "\(currentDateformat) 07:45:00 PM"
let compareDate1 = "\(currentDateformat) 07:45:00"
if dateString == compareDate {
checkString = "equal date"
}
if dateString1 < compareDate1{
checkString = "greater than"
} else {
checkString = "less than"
}
return checkString
}
dateString和comparedDate都以UTC显示其值。东部标准时间和UTC之间有4小时的偏移。那么我如何计算东部标准时间区的准确时间呢。我应该从日期中减去4小时吗?你不应该手动减去时间,我编辑了我的答案,希望它能有所帮助。
if let currentDateAndTime = currentDateFormatter.date(from: dateString) {
print(currentDateFormatter.string(from: currentDateAndTime))
}
func getCurrentDateTime() -> String {
var checkString : String!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST")
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss a"
let dateString:String! = dateFormatter.string(from: Date())
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone(abbreviation: "EST")
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateString1:String! = dateFormatter1.string(from: Date())
let convertDateFormatter = DateFormatter()
convertDateFormatter.dateFormat = "dd-MM-yyyy"
convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
let currentDateformat = convertDateFormatter.string(from: Date())
let compareDate = "\(currentDateformat) 07:45:00 PM"
let compareDate1 = "\(currentDateformat) 07:45:00"
if dateString == compareDate {
checkString = "equal date"
}
if dateString1 < compareDate1{
checkString = "greater than"
} else {
checkString = "less than"
}
return checkString
}