0

I need to get the current time in a 24 hour format to compare to other nsdate objects I created from a string. In thid case I dont have a string time so i tried created one but I cant get it to work. This is my code:

 let now = NSDate()
   let dateFormatter = NSDateFormatter()
   dateFormatter.timeZone = NSTimeZone.localTimeZone()
   dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
    let calendar = NSCalendar.currentCalendar()
    let dateString = NSDateFormatter.localizedStringFromDate(now, dateStyle: .ShortStyle, timeStyle: .ShortStyle)
    let currentTime = dateFormatter.dateFromString(dateString)
    print(currentTime)

current time is nil, any help is appreciated.

Gama
  • 352
  • 2
  • 16

1 Answers1

2

Replace all of the code in your question with just:

let currentTime = NSDate()

That's it. Use that currentTime to compare to your other NSDate objects.

There is no need for all of the date formatting and conversions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • but when i print current time it prints it in a different format and the time is not right either, it prints "2016-04-13 05:18:20 +0000" and it is 11:18 of april the 12 th – Gama Apr 13 '16 at 05:18
  • That's just the standard output when you log an `NSDate`. Don't worry about that, it's irrelevant. `currentTime` represents "now". – rmaddy Apr 13 '16 at 05:19
  • will it make any difference if I'm comparing it to a nsdate with a 24 hour format? – Gama Apr 13 '16 at 05:21
  • 1
    What do you mean by "an `NSDate` with a 24-hour format"? An `NSDate` represents a point in time. If you have two `NSDate` objects, you can simply compare them with the `compare:` function. Try it. – rmaddy Apr 13 '16 at 05:23
  • I'll try it, just curious though, when i print my other nsdate, it prints with a 24 hour format "2016-05-06 14:00:00 +0000" – Gama Apr 13 '16 at 05:24
  • You get that format when printing any `NSDate` object. – rmaddy Apr 13 '16 at 05:25
  • ok im getting confused here, when i print currentTime = NSdate() i get "2016-04-13 05:18:20 +0000" and when i print my other nsdate object ""2016-05-06 14:00:00 +0000" i get a 24 hour format lol. – Gama Apr 13 '16 at 05:27
  • What time zone are you in? UTC-6? 05:18:20 is a 24 hour format - it is currently 5:29 am UTC – Paulw11 Apr 13 '16 at 05:27
  • @GamalielTellezOrtiz Those are both being printed in 24-hour time. Why do you think one is and one isn't? – rmaddy Apr 13 '16 at 05:28
  • You compare two same NSDate? – Twitter khuong291 Apr 13 '16 at 05:29
  • nope, Im at mountain time and it is 11:30 still the 12th, the currentTime = NSdate prints that is it is already the 13th " "2016-04-13 05:18:20 +0000". That is why I dont feel confident to just use nsdate() lol – Gama Apr 13 '16 at 05:31
  • Look at the output. It's printing your current time (11:30 PM MDT) in UTC time. Since you are 6 hours behind UTC, 11:30 PM MDT is the same time as 05:30 AM UTC the next day. It's the same moment in time, just logged with a different timezone. – rmaddy Apr 13 '16 at 05:33
  • So your local time is 2016-04-12 23:30:00 -0600 - This is the same as 2016-04-13 05:30:00 +0000 – Paulw11 Apr 13 '16 at 05:34
  • i see that, so it doesnt matter if i compare two nsdate with different time zone? lol – Gama Apr 13 '16 at 05:37
  • 2
    `NSDate` doesn't have timezones. A timezone is only relevant when converting the `NSDate` to a string for display. When you compare two `NSDate`, the timezone is irrelevant. – rmaddy Apr 13 '16 at 05:39