How to get the time in am/pm format iphone NSDateFormatter?

IphoneIosTimeNsdateformatter

Iphone Problem Overview


Am working in an iPhone app with using the NSDateFormatter. Am getting the time from the webservice like "00:00:00", "16:00:00","15:30:00" and so on. I need to convert these time to if the time is "00:00:00" to "12 AM", if the time is "16:00:00" to "4 PM", if the time is "15:30:00" to "3.30 PM". I have used the below code to this but when i have pass "16:00:00" the date returns null.

 NSString *dats1 = @"16:00:00";
 NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];  
 [dateFormatter3 setDateFormat:@"hh:mm:ss"]; 
 NSDate *date1 = [dateFormatter3 dateFromString:dats1];
 NSLog(@"date1 : %@", date1); **// Here returning (null)**
                
 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"hh:mm a"];
 NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
 [formatter release];

When am passing "00:00:00" it is returning "12 AM" correctly. Can anyone please help me to solve this issue? Thanks in advance.

Iphone Solutions


Solution 1 - Iphone

If your time format is in 24hrs please use "HH:mm:ss" format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"

NSString *dats1 = @"16:00:00";
 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];  
 [dateFormatter3 setDateFormat:@"HH:mm:ss"]; 
 NSDate *date1 = [dateFormatter3 dateFromString:dats1];
 NSLog(@"date1 : %@", date1); **// Here returning (null)**

 [dateFormatter setDateFormat:@"hh:mm a"];
 NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
 [formatter release];

Thanks.

Solution 2 - Iphone

You can use the .ShortStyle which will adapt to the current locale:

let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())

Which returns 17:12 or 5:12 PM depending on the current device locale.

Solution 3 - Iphone

Why are you again allocating the NSDateFormatter ?

you Just need to format it to NSDate and then again to NSString,

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    
    NSDate *date = [dateFormatter dateFromString:dats1];
    
    [dateFormatter setDateFormat:@"hh:mm a"];
    
    NSString *formattedDate = [dateFormatter stringFromDate:date];

    NSLog(@"%@",formattedDate);

Solution 4 - Iphone

Try HH:mm:ss (notice the capitals in the hour format)

Solution 5 - Iphone

Hope someone would be helpful who would like to get it in Swift

Swift 5.1+

let dateFormatter = DateFormatter()
//if you want 12AM,5PM then use "hh mm a"
//if you want 00AM,17PM then use "HH mm a"
dateFormatter.dateFormat = "hh mm a"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
                

Solution 6 - Iphone

This Below code always keep in 12hour format:

NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.locale = 12hourFomrat;

This always keep in 24 hour format

NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
df.locale = 24hourFormat

Solution 7 - Iphone

your date format should be:

  [dateFormatter3 setDateFormat:@"HH:mm:ss"]; 

check the Date Format Pattern Doku

Solution 8 - Iphone

Swift 5:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGopinathView Question on Stackoverflow
Solution 1 - IphoneYuvaraj.MView Answer on Stackoverflow
Solution 2 - IphoneeliocsView Answer on Stackoverflow
Solution 3 - IphoneCharanView Answer on Stackoverflow
Solution 4 - IphoneSteven KramerView Answer on Stackoverflow
Solution 5 - IphoneRafat touqir RafsunView Answer on Stackoverflow
Solution 6 - IphoneMutablegopiView Answer on Stackoverflow
Solution 7 - IphoneCarlJView Answer on Stackoverflow
Solution 8 - IphoneSreedeepkesav M SView Answer on Stackoverflow