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

I am working in an iPhone application using NSDateFormatter. I get time from a web service, for example, "00:00:00", "16:00:00","15:30:00" and so on . I need to convert this 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 used the code below, but when I have the 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 I go through โ€œ00:00:00โ€, it returns โ€œ12 AMโ€ correctly. Can someone help me solve this problem? Thanks in advance.

+44
ios time iphone nsdateformatter
Jul 23 '12 at 7:32
source share
7 answers

If your time format is 24hrs , use the format "HH:mm:ss" . Please check the code and let me know if it works for you. I tested and returns "4 PM"

 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]; 

Thank.

+101
Jul 23. '12 at 7:40
source share

Why are you highlighting NSDateFormatter again?

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); 
+15
Jul 23 2018-12-12T00:
source share

Try HH:mm:ss (note capitals in hourly format)

+14
Jul 23 2018-12-12T00:
source share

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

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

Returns 17:12 or 5:12 PM depending on the current locale of the device.

+9
Jan 20 '16 at 2:48
source share

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

For swift

 let dateFormatter = NSDateFormatter() //if some one want 12AM,5PM then use "hh mm a" //if some one want 00AM,17PM then use "HH mm a" dateFormatter.dateFormat = "hh mm a" let stringDate = dateFormatter.stringFromDate(NSDate()) println(stringDate) 
+6
Apr 22 '15 at 10:54
source share

This code below is always stored in 12hour format:

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

It is always saved in 24 hour format.

 NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]; df.locale = 24hourFormat 
+6
Aug 07 '15 at 10:33
source share

Your date format should be:

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

Check Doku Date Format

+3
Jul 23 '12 at 7:40
source share



All Articles