0

I know its simple but I cant just get it right. Have been staring at this long time. I have a string with 03AM but when I try to get date from it I cannot get it working. It gives me 1970-01-01 21:00:00 +0000. dont know why. Can anyone help out here :

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"ha"];
    [dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease] ];
    NSDate *date1 = [dateFormat dateFromString:display];
    NSLog(@"Display 2 : %@", date1);
lifemoveson
  • 1,661
  • 8
  • 28
  • 55

2 Answers2

1
[dateFormat setDateFormat:@"ha"];

Should be:

[dateFormat setDateFormat:@"hh a"];

See if that works ;)

ref: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • how are you filling display in: NSDate *date1 = [dateFormat dateFromString:display];? – Totumus Maximus Oct 14 '11 at 18:21
  • I am getting it from the database and NSString *display = [eachElement objectAtIndex:10];. I have checked the value here it comes as 3 PM. but dont know why its coming as entire date. – lifemoveson Oct 14 '11 at 18:23
  • @TotumusMaximus I have data as 3 PM in my database. Can I convert that to NSDate ? – lifemoveson Oct 14 '11 at 18:35
  • It comes as an entire date because either the format isn't done correctly. If it has no formatting behaviour it will use the default one. Read up on how to use dateformats again: http://lukeredpath.co.uk/blog/parsing-strings-with-nsdateformatter-gotcha.html I'm sure you will figure it out. – Totumus Maximus Oct 14 '11 at 18:37
  • But then is there a way to just get part of date from a string ? Like I have a date 2011-10-01 9:30 AM. I just need 9:30 AM from the string as my NSDate. Is it possible ? Like NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateFormat:@"h:mm a"]; – lifemoveson Oct 14 '11 at 18:43
0

To get AM/PM to display correctly you may have to set the locale to a special value. See this thread.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151