来自字符串的日期没有给出正确的日期

Date from String is not giving correct date

我想将 "EST" 设置为每个用户的默认时区,但有一个条件需要在当前日期 7:45 下午检查。所以我正在比较两个日期,但问题是当我将当前日期转换为字符串时,它会给我正确的 EST 时间,当我再次将该字符串转换为 EST 中的日期时,它会给我比 EST 提前 4 小时的时间。这是转换代码

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

}

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())

    let convertDateFormatter = DateFormatter()
    convertDateFormatter.dateFormat = "dd-MM-yyyy"
    convertDateFormatter.timeZone = TimeZone(abbreviation: "EST")
    let currentDate = convertDateFormatter.string(from: Date())

    let comparedDateFormatter = DateFormatter()
    comparedDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
    let comparedDate = comparedDateFormatter.date(from: "\(currentDate) 19:45:00")

    let currentDateFormatter = DateFormatter()
    currentDateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
    let currentDateAndTime = currentDateFormatter.date(from: dateString)

    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
}

如果我试图显示这些日期,我会使用日期格式化程序将它们转换为字符串。

其实我没发现代码有什么问题。

Date 始终处于 UTC 时区。 DateFormatter 发挥了作用。

按照格式打印日期: 使用 string(from:) 方法。

if let currentDateAndTime = currentDateFormatter.date(from: dateString) {
    print(currentDateFormatter.string(from: currentDateAndTime))
}

NB: When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string. For most fixed formats, you should also set the locale property to a POSIX locale ("en_US_POSIX"), and set the timeZone property to 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
}