5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RJ168
  • 1,006
  • 2
  • 12
  • 22

6 Answers6

22

Use below lines of code

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"hh:mm a"];   
    NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"str_date:%@",str_date);
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
  • Thank u, this solved my issue :) I want to ask you question: how to parse date came from API like these 2014-03-06T00:00:00+0200 This is my code: dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; but it came with nil all the time, Can you help me ? – Atef Mar 09 '14 at 10:02
4

You could use this document for more information NSDateFormatter Class Reference .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]);
Hari
  • 1,509
  • 15
  • 34
  • Yes it help me very much ..... i actually i just want to adjust the size of date picker with 12hr and 24hr time format .. and its really help me .. Thank you – RJ168 Nov 19 '11 at 12:13
2

You can set your DateFormatter amSymbol and pmSymbol as follow:

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: Objective C Code

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

Ref link: https://stackoverflow.com/a/31469237/2905967

Manab Kumar Mal
  • 20,788
  • 5
  • 31
  • 43
1

Date to String

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

Date Format Symbols The table from the unicode date formatting page should be enough for you to build your own desired 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 datails.

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

this really helped me. It was really easy.

TheRealRonDez
  • 2,807
  • 2
  • 30
  • 40
0

for printing AM/ PM the parameter a/aa/aaa all 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);
triandicAnt
  • 1,328
  • 2
  • 15
  • 40
0

You can use in swift 5

    var strDate1:String = "2019-12-19 05:42:04";
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let dateObj = dateFormatter.date(from: strDate1)
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss a"
    let strigDate =  dateFormatter.string(from: dateObj!)

Output: 2019-12-19 05:42:04 AM

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50