-2

I need to change string that is in 12 hour time format into NSDate. I am using below given code but its returning null. Can anyone help me with this??

NSString = @"04/03/2013 7pm";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"dd/MM/yyyy hh:mm a"];
NSDate *date = [df dateFromString:selectedDateString];
  • The date format you are using doesn't match the format of the string. Use `[df setDateFormat:@"dd/MM/yyyy hha"];` There is no colon or minutes in the string so you can't have them in the date format. – rmaddy Mar 01 '13 at 05:33

1 Answers1

5

It is giving you null as your DateFormat does not match to your string. Try this,

    NSString *selectedDateString= @"04/03/2013 7pm";
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"dd/MM/yyyy ha"];
    NSDate *date = [df dateFromString:selectedDateString];
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • @user2107025, If it worked for you, you can mark it as accepted by clicking on the checkmark next to the answer. http://stackoverflow.com/about – iDev Mar 01 '13 at 06:02