8

I am using firebase as a backend and storing a string of time like this 7:00 PM.

I am trying to convert the string received from Firebase and convert it into NSDate so I can sort it, change the time..etc

I've looked online and come up with this code so far

dateFormatter.dateFormat = "hh:mm a"
                  dateFormatter.locale = NSLocale.current
                  dateFormatter.timeZone = NSTimeZone.local
                  let date = dateFormatter.date(from: item)
                  self.times.append(date!)
                  print("Start: \(date)")

where item is the string (7:00 PM)

When I run the app, the console returns:

Item: 9:00 AM

Start: Optional(2000-01-01 05:00:00 +0000)

Ive set timezone, locale, format. Why is the time being returned not correct?

A few other examples printed out:

Item: 1:20 PM

Start: Optional(2000-01-01 17:20:00 +0000)

Item: 9:40 AM

Start: Optional(2000-01-01 05:40:00 +0000)

Item: 10:00 AM

Start: Optional(2000-01-01 05:00:00 +0000)

Item: 12:00 PM

Start: Optional(2000-01-01 17:00:00 +0000)

huddie96
  • 1,240
  • 2
  • 13
  • 26
  • For a start, the format for 12 hour am/pm time is "hh", not "HH" – Martin R Feb 19 '17 at 21:42
  • That was actually a mistake, I did have it as hh:mm a (Edited the post) – huddie96 Feb 19 '17 at 21:43
  • What is your timezone? – Martin R Feb 19 '17 at 21:45
  • I guess that you live in a 5 hours difference from GMT+0? Right? That explains the hour issue. For the year/month/day, it's because your string doesn't contains any information about it, so it's putting it a the reference 2000. – Larme Feb 19 '17 at 21:46
  • Yes I am. How do I fix it then? Is there anyway to have it show 9:00 AM no matter what ? – huddie96 Feb 19 '17 at 21:48
  • A `Date` is an absolute point in time and has no time zone. To display it as "9:00 AM" you have to convert it back to a string using a date formatter. – Possible duplicate of http://stackoverflow.com/questions/29407599/nsdateformatter-return-wrong-date-swift, http://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-by-a-few-hours and some more – Martin R Feb 19 '17 at 21:49
  • So removing the timezone when converting to NSDate and when converting back to String will solve the problem? For example: Convert 9:00 PM (String) to 9:00 PM (Date) add 2 hours and convert back to a string will return 11:00 PM? – huddie96 Feb 19 '17 at 21:53
  • @MartinR hh:mm a is what I had before but the app crashes. It doesn't crash when HH:mm a – huddie96 Feb 19 '17 at 22:07

1 Answers1

19

Always remember this: Date / NSDate stores times in UTC. If your timezone is anything but UTC, the value returned by print(date) will always be different.

You can make it print out the hour as stored in Firebase by specifying a UTC timezone. The default is the user's (i.e. your) timezone:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

let item = "7:00 PM"
let date = dateFormatter.date(from: item)
print("Start: \(date)") // Start: Optional(2000-01-01 19:00:00 +0000)
Code Different
  • 90,614
  • 16
  • 144
  • 163