3

I have a string @"21:57" and I want to save it with the same HH:mm format in NSDate. I tried converting it using:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"HH:mm";
NSDate *date  = [dateFormatter dateFromString:stringToConvertFrom];

but I'm getting 2000-01-01 21:57:00 +0000 instead of 21:57.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vinita
  • 95
  • 1
  • 3
  • 9
  • 1
    Sounds right to me. (Remember, an NSDate object contains no timezone or format -- it always displays in the same format and in UTC/GMT.) – Hot Licks Sep 10 '13 at 16:25
  • If you just want a time string then why convert it to an `NSDate`? An `NSDate` is a full timestamp. You will always get a date and time unless you format the output using an `NSDateFormatter`. – rmaddy Sep 10 '13 at 16:32
  • I want to create an array of objects in which one property is the time I'm converting. Then I want to use predicate to find the object with time greater than a particular time. – Vinita Sep 10 '13 at 16:37
  • 1
    You should take a look at `NSDateComponents` class: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSDateComponents_Class/Reference/Reference.html – Raúl Juárez Sep 10 '13 at 20:55

0 Answers0