0

I know this will be a silly question, but I tried everything and still couldn't achieve what I wanted to do.

This is my question:

I want to check two time values and a date, I have string which is having time like this "05.30 AM". I want to convert it into time format [it is better if i can convert exactly the way it is now]. I know there is a way which can compare two time values using:

NSComparisonResult result = [date1 compare:date2];.

I have followed below code to convert my string value to the time format but it is only returns null.

Please guide me for this issue:

NSString* tim = @"11:12 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];

NSDate *formatedTime= [formatter dateFromString:tim];
NSLog(@"TIME : %@",formatedTime);
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
Darshana
  • 314
  • 2
  • 4
  • 22

4 Answers4

1

For this particular case (with tim formatted the way you have it) you just missing one line: [formatter setDateStyle:NSDateFormatterNoStyle]; this will tell formatter not to look for date in your string.

NSString* tim = @"11:12 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterNoStyle];

NSDate *formatedTime= [formatter dateFromString:tim];
NSLog(@"TIME : %@",formatedTime);
dariaa
  • 6,285
  • 4
  • 42
  • 58
  • i tried your answer sir.. but still i get value like this.2000-01-01 05:12:00 +0000 – Darshana Mar 18 '14 at 07:42
  • if formatter will not look for the date.. then why i still get an date for my out put..and why my time is incorrect? i cant understand sir – Darshana Mar 18 '14 at 07:43
0

You need set the format.

[formatter setDateFormat:@"hh:mm a"];
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • thank you.. now i got some value.. but the time is wrong...i pass 11:12 AM.. but it returns 2000-01-01 05:12:00 +0000.. how can i fix this – Darshana Mar 18 '14 at 07:34
  • 1
    I think this is because the date doesnt hold time zone, try to set it. `[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];` – Tarek Hallak Mar 18 '14 at 07:46
  • you were right sir..now i can get output like this....2000-01-01 11:12:00 +0000.. but still i have a problem..how can we check this is AM or PM...? – Darshana Mar 18 '14 at 07:50
  • 1
    You can control this when printing the date, i think it is 24 hours date, try to set another formatter for printing the date, [outputFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];. – Tarek Hallak Mar 18 '14 at 07:54
0
NSString* tim = @"11:12 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm a"];

NSDate *formatedTime= [formatter dateFromString:tim];
NSLog(@"TIME : %@",formatedTime);

But you will receive

TIME : 1999-12-31 23:12:00 +0000

since year is unknown.

Avt
  • 16,927
  • 4
  • 52
  • 72
0

First you need to format your String like this

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss ZZZ";
[formatter dateFromString:@"Fri, 25 Jan 2014 5:30:22 +0000"];
Chathurka
  • 605
  • 6
  • 11