Converting string to date returning the day before

2019-07-17 23:18发布

I have a date in a string with this format "2017-03-14" (yyyy-MM-dd) and i am trying to convert it into a string with this format "Tuesday, March 14, 2017".

This is my code:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-03-14")

Now if i print the value of "date", i get this:

2017-03-13 22:00:00 +0000

This is just the day before. Why is that ?

EDIT:

I need to compare date before formatting it.

var newDateString : String = ""

let date2 = Date()

let comp = date2.compare(date!)

if comp.rawValue == 0 {
    newDateString = "TONIGHT"
} else {
    dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
    newDateString = dateFormatter.string(from: date!)
}

Thanks

3条回答
女痞
2楼-- · 2019-07-17 23:32

Your confusion is based on a misunderstanding of what Time and Date are. Evidently, you are currently located in a time zone that is 2 hours ahead of UTC (Coordinated Universal Time), previously known as Greenwich Mean Time (GMT).

When you ask the OS for a Date object converted from "2017-03-14" you get a date/time reference of Midnight the morning of 2017-03-14 in your time zone which is, correctly, 10:00 pm (22:00) then night before in UTC.

When you ask the OS for a Date object for now with Date() you get a date/time reference of now in your time zone, which will be two hours earlier in UTC.

To accurately evaluate your date string to say "is now earlier than 'tonight of 2017-03-14'" you will probably want to convert from "2017-03-14 23:59" (or 11:59 pm, or perhaps prior to the start of tonight's event of 8:00 pm, etc).

This will do your original comparison, but would work better as a function (although I'm not sure how you want to use it)...

var newDateString : String = ""

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"

// set tonightDate to 1 minute before midnight, tonight, in local time
if let tonightDate = dateFormatter.date(from: "2017-03-14 23:59") {

    // set nowDate to current local time
    let nowDate = Date()

    let comp = nowDate.compare(tonightDate)

    if comp.rawValue <= 0 {
        newDateString = "TONIGHT"
    } else {
        dateFormatter.dateFormat = "EEEE, MMMM d, yyyy"
        newDateString = dateFormatter.string(from: tonightDate)
    }

}

print(newDateString)
查看更多
Fickle 薄情
3楼-- · 2019-07-17 23:34

The desired Format should be:

EEEE, MMMM dd, yyyy

All you have to do is to add after your code the following code snippet:

dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
let string = dateFormatter.string(from: date!) // "Tuesday, March 14, 2017"

Remark:

I'd like to suggest to do optional binding for declaring the date, as follows:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

if let date = dateFormatter.date(from: "2017-03-14") {
    dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
    let string = dateFormatter.string(from: date) // "Tuesday, March 14, 2017"
}
查看更多
何必那么认真
4楼-- · 2019-07-17 23:38

It is calculated time based on UTC so you are getting day before.You can get proper format using below code:

  func chageDateFormat(date:String) -> String{
  let dateFormatter = NSDateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd"

  guard let date = dateFormatter.dateFromString(date) else {
    assert(false, "No date from string")
    return ""
  }
  print(date)
  dateFormatter.dateFormat = "EEEE, dd MMMM, yyyy"
  let result = dateFormatter.stringFromDate(date)
  return result
}

let resultFormateDate = chageDateFormat("2017-03-14")  

For comparison you also need to convert Date() to proper format.Both date must be in same format.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = NSDate()
let strdate = dateFormatter.stringFromDate(date)

let resultFormateDate2 = chageDateFormat(strdate)

Now you can compare two strings

if resultFormateDate == resultFormateDate2{
 print("True")
}
查看更多
登录 后发表回答