Reputation: 79
When i am
[dateFormatter setDateFormat:@"HH:mm"]
using to fetch the current hour i am getting correct time till 12:59 but after that it again starts from 1:00 i.e. 12:59 is displayed as 12:59 but 14:00 is displyed as 2:00.
Please let me if anyone is also having a similar problem and suggest a solution.
Thanks
Upvotes: 0
Views: 606
Reputation: 2453
Try this code
[dateFormatter setDateFormat:@"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss a"];
Hope it works for you.
Upvotes: 0
Reputation: 2337
You can use
[dateFormatter setDateFormat:@"H:mm"]
for 24 hour time format
Upvotes: 0
Reputation: 200
Use this one
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd kk:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:date];
Upvotes: 0
Reputation: 208
Please check the code below.
NSDateFormatter *dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateStyle = NSDateFormatterMediumStyle;
dateFormator.dateFormat=@"HH:mm:ss";
NSCalendar * calender=[[NSCalendar alloc]initWithCalendarIdentifier:[NSGregorianCalendar autorelease]];
NSDateComponents * component=[calender components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:date];
Upvotes: 3
Reputation: 1623
I agree with
[dateFormatter setDateFormat:@"HH:mm"]
should give you the time in 24 Hours. But this doesn't work if the user set 24 hour to off. Despite using HH in a format string, the user setting overrides this. This might be the problem in your case!
Upvotes: 1