Question

I have an issue when converting time from 24 hour time style to 12 hour time style. When you see iOS standard for Japanese time representation it shows 12 hour time showing 0 to 11.
But while converting using inbuilt method of iOS it gives 午後12:00or午前12:00 when you convert 00:00. When you see alarm application with setting 12 hour time style and region japanese, you will see time display 0 to 11. But when you use inbuilt method of iOS it gives different result.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [DateUtility createLocale];
    locale=[[NSLocale alloc]initWithLocaleIdentifier:@"ja_JPPOSIX"];
    [dateFormatter setLocale:locale];
    dateFormatter.dateFormat = @"hh:mm";
    NSDate *date = [dateFormatter dateFromString:time];
    dateFormatter.dateFormat = @"a";
    NSLog(@"Meridian=%@",[dateFormatter stringFromDate:date]);
    dateFormatter.dateFormat = @"hh";
    NSLog(@"Hour=%@",[dateFormatter stringFromDate:date]);
Was it helpful?

Solution 2

Use dateformate "KK" to get hour range 0 to 11.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [DateUtility createLocale];
locale=[[NSLocale alloc]initWithLocaleIdentifier:@"ja_JPPOSIX"];
[dateFormatter setLocale:locale];
dateFormatter.dateFormat = @"KK:mm";
NSDate *date = [dateFormatter dateFromString:time];
dateFormatter.dateFormat = @"a";
NSLog(@"Meridian=%@",[dateFormatter stringFromDate:date]);
dateFormatter.dateFormat = @"KK";
NSLog(@"Hour=%@",[dateFormatter stringFromDate:date]);

OTHER TIPS

If you are looking for a way to convert your date format to a very specific style, I would suggest using the NSDateFormatter class.

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormate: @"yyyy-MM-dd HH:mm:ss"]; //This is just an example format, from one of my projects. It looks like this: "2014-05-07 18:32:25"

NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];

The dateString should now have a string with the current time in a format of your choice.

For a full list of all the formats you can use, check this link. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top