-1

I have two times both in String format:

timeOne = "12:58 AM"
timeTwo = "1:05 PM"

I convert them into NSDate like this

func convertTime(tim: String) -> NSDate{
let strDate = tim
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm a"
//converts into NSDate so we can find the time remaining
return (dateFormatter.dateFromString(strDate))!
}

let tOne = convertTime(timeOne)
let tTwo = convertTime(timeTwo)

converting tOne and tTwo outputs me

tOne = 2000-01-01 00:58:00 +0000
tTwo = 2000-01-01 12:05:00 +0000

I compare the two times but i want it to return 7 mins whereas im getting '667' The code to compare is:

let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Minute, fromDate: tOne, toDate: tTwo, options: [])
print(components)

This prints out

667

However it works if the both times are the same AM or PM What i mean by this is that lets say

tOne = "1:06 PM"
tTwo = "1:15 PM"

running the comparison code it successfully outputs "9"

Minhal Khan
  • 301
  • 3
  • 11
  • What's the value of `tOne` and `tTwo` in your example? – Larme Jul 25 '16 at 12:29
  • 5
    I think you are mistaken in your use of a.m. - 12:58 a.m. is only meaningful as 00:58, not 12:58, which would be p.m. = *post meridian* - i.e. "after noon". Therefore it is 11 hours 7 minutes = 667 minutes between the times. – Grimxn Jul 25 '16 at 12:31
  • I edited it check the value – Minhal Khan Jul 25 '16 at 12:32
  • Try and use your code in Playground, your output is `tOne = 12:58 AM` and `tTwo = 12:05 PM` – Idan Jul 25 '16 at 12:32
  • How would i fix this 11 hours 7 minute? – Minhal Khan Jul 25 '16 at 12:33
  • 3
    what do u want to fix? it is 11 hours and 7 minutes. – vikingosegundo Jul 25 '16 at 12:35
  • so i have 12:58 AM and 1:05 PM find the correct minutes left from tOne and tTwo – Minhal Khan Jul 25 '16 at 12:37
  • 3
    the correct time difference: 11 hours and 7 minutes. – vikingosegundo Jul 25 '16 at 12:38
  • How would i change it so it can out put 7 minutes? – Minhal Khan Jul 25 '16 at 12:38
  • give it dates that have 7 minutes difference, like "12:58 PM" and "1:05 PM" – vikingosegundo Jul 25 '16 at 12:39
  • but it is 7 minutes in the 'real world' because 7 minutes add 12:58 = 1:05? – Minhal Khan Jul 25 '16 at 12:40
  • Transform `12:58 AM` to `12:58 PM` It should gives your 7 minutes. – Larme Jul 25 '16 at 12:42
  • 2
    12:58 *AM* is just 58 minutes past midnight. 1:05 *PM* is 65 minutes past noon. did you now get it? – vikingosegundo Jul 25 '16 at 12:43
  • @MinhalKhan: Compare https://en.wikipedia.org/wiki/12-hour_clock. – Martin R Jul 25 '16 at 12:43
  • 1
    You don't understand what `12:58 AM` means. Understand what "real time" it is, then come back. That's why I invited you to check the values of `tOne`, it was an hint to let you understand. I quite guess that you are use a a 24h format and not a AM/PM one, but if you don't understand it, you can't work with it. The "7" minutes you want, is either because the original infos of `12:58 AM` is wrong, or if it's correct, then the REAL difference between the two hours is 667 minutes. – Larme Jul 25 '16 at 12:45
  • 3
    Actually the date format for the 12-hour clock is **"hh:mm a"** (with lowercase "hh"), and with that you'll get the correct difference of **727 minutes** (12 hours and 7 minutes). – Martin R Jul 25 '16 at 12:50
  • OHHHHH sorry cause i have never used time in xcode so i really wouldnt understand it, but i got it Thanks! everyone – Minhal Khan Jul 25 '16 at 12:52

1 Answers1

1

your understanding of am and pm times seems to be false.

The times aren't 7 minutes in difference, the first is just after midnight, the second 65 minutes after noon.

enter image description here

If you fix your code as suggested by MartinR, the correct difference will be 12 hours and 7 minutes.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178