How to print time with AM / PM format on iPhone?

I want to print the time from the date, but with AM / PM. I can print time, but it never prints AM / PM. How can i do this?

+4
source share
5 answers

You can use this document for more information. NSDateFormatter class help . An example could be:

NSDate* date = [NSDate date]; NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"MM/dd/yyyy"]; [formatter setTimeStyle:NSDateFormatterFullStyle]; NSLog(@"date=%@",[dateFormatter stringFromDate:date]); 
+3
source

Use the lines below the code

  NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"hh:mm a"]; NSString *str_date = [dateFormatter stringFromDate:[NSDate date]]; NSLog(@"str_date:%@",str_date); 
+20
source

Date for string

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MMM"]; cell.dateLabel.text = [formatter stringFromDate:item.pubDate] 

Date format characters The table from the Unicode date format page should be enough for you to create your own date format string ...

 Pattern Result (in a particular locale) yyyy.MM.dd G 'at' HH:mm:ss zzz 1996.07.10 AD at 15:08:56 PDT EEE, MMM d, ''yy Wed, July 10, '96 h:mm a 12:08 PM hh 'o''clock' a, zzzz 12 o'clock PM, Pacific Daylight Time K:mm a, z 0:00 PM, PST yyyyy.MMMM.dd GGG hh:mm aaa 01996.July.10 AD 12:08 PM Hope this is useful to someone out there. 

Use the following link for more information.

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

It really helped me. It was very easy.

+1
source

You can set the DateFormatter amSymbol and pmSymbol as follows:

Xcode 8.3 • Swift 3.1

 let formatter = DateFormatter() formatter.dateFormat = "h:mm a 'on' MMMM dd, yyyy" formatter.amSymbol = "AM" formatter.pmSymbol = "PM" let dateString = formatter.string(from: Date()) print(dateString) // "4:44 PM on June 23, 2016\n" 

Edit: Goal Code C

 NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.timeZone = [NSTimeZone systemTimeZone]; df.AMSymbol = @"AM"; df.PMSymbol = @"PM"; df.dateFormat = "h:mm a 'on' MMMM dd, yyyy" NSString *stringDate = [df stringFromDate:dateFromString]; 

Link Link: fooobar.com/questions/1382034 / ...

+1
source

for AM / PM printing, a/aa/aaa will work.

 NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [myDateFormatter setDateFormat:@"hh:mm aaa"]; //a,aa or aa all will work. [myDateFormatter setDateFormat:@"hh:mm aa"]; NSString *myDate = [myDateFormatter stringFromDate:[NSDate date]]; NSLog(@"myDate is :%@",myDate); 
0
source

Source: https://habr.com/ru/post/1382032/


All Articles