Sumeet
Sumeet

Reputation: 117

Date conversion issue in ios

I have an issue of converting time into 24 hours from 12 hours. For this I did all the required things and followed mostly all the stack overflow answers but it didn't help me. What exactly I need to do is I have a NSString object having time (not date) in 12 hours like 6:00 pm and I need to change this in 24 hours (18:00). Here is what I did for this :

NSString* dateString = startTime;
NSDateFormatter* fmt = [NSDateFormatter new];
[fmt setDateFormat:@"HH:mm"];
NSDate* date = [fmt dateFromString:dateString];

But it returns me nil date. What can be missing here?

Update :

One more thing,12 hours time gives me correct result but 'am' and 'pm' are small letters so is there any procedure to have these in capital letters.

Upvotes: 0

Views: 318

Answers (1)

Raymond Li
Raymond Li

Reputation: 22

NSString* dateString = @"6:00 pm";
NSDateFormatter* dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:@"h:mm a"];
NSDate* date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:@"H:mm"];
return [dateFormat stringFromDate: date];

Upvotes: 1

Related Questions