字符串中的日期未给出正确的日期

时间:2018-07-27 04:56:58

标签: ios string date swift4 nsdateformatter

我想将“ EST”设置为每个用户的默认时区,但是有一个条件需要在当前日期晚上7:45进行检查。所以我在比较两个日期,但是问题是当我将当前的Date转换为String时,它给了我正确的EST时间;当我在EST中再次将该String转换为Date时,给了我比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

}

4 个答案:

答案 0 :(得分:1)

因此,日期没有时区。 因此,当我使用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
}

如果我试图显示这些日期,我将使用日期格式器将其转换为字符串。

答案 1 :(得分:1)

我根据您的要求修改了代码:

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
}

答案 2 :(得分:0)

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

}

请检查此代码。

答案 3 :(得分:0)

实际上,我没有发现任何错误。

Date始终处于UTC时区。 DateFormatter发挥了魔力。

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

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

注意:使用固定格式的日期(例如RFC 3339)时,需要设置   dateFormat属性以指定格式字符串。对于大多数固定   格式,还应该将locale属性设置为POSIX语言环境   (“ en_US_POSIX”),并将timeZone属性设置为UTC。

相关问题