Renê Lima
Renê Lima

Reputation: 27

Different time database

See how this time in my database (I'm using parse.com):

2015-08-11 18:00:00 +0000

2015-08-11 18:00:00 +0000

My code:

// Data no detalhamento
var endDateFormatter = NSDateFormatter()
endDateFormatter.locale = NSLocale(localeIdentifier: "pt_BR")
endDateFormatter.dateFormat = "E, dd MMM yyyy HH:mm:ss"
endDateFormatter.timeZone = NSTimeZone.localTimeZone()

let endDateStr = endDateFormatter.stringFromDate(eventObj[EVENTS_END_DATE] as! NSDate)



if endDateStr != "" {  endDateLabel.text = "Sorteio: \(endDateStr)"
} else { endDateLabel.text = "Sorteio:"  }

More see how it looks in the application:

enter image description here

What could be happening?

Upvotes: 0

Views: 52

Answers (2)

Leo Dabus
Leo Dabus

Reputation: 236458

"2015-08-11 18:00:00 +0000" means 6pm at UTC. Brazil's local time offset is minus 3 hours, that's why your date at local time shows 15:00 (3pm).

let endDateString = "2015-08-11 18:00:00 +0000"

let endDateFormatter = NSDateFormatter()
endDateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
endDateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
endDateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
endDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xx"
if let dateFromString = endDateFormatter.dateFromString(endDateString) {
    print(dateFromString)   // "2015-08-11 18:00:00 +0000"
}

enter image description here

Upvotes: 1

Argee
Argee

Reputation: 1224

Okay just a guess, but...

Dates in Parse are usually UTC. If you set a different timezone, it should change the hour-part of your Date...

Upvotes: 0

Related Questions