NSDateFormatter- stringFromDate not returning AM/PM
Asked Answered
N

6

12
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm:ss a"];
    NSLog(@"Today's Time: %@", [formatter stringFromDate:[NSDate date]]);

The above code just gives the following output Today's Time: 15:46:43 I want the output to be Today's Time: 3:46 PM. I surfed a lot and i did not find a way to show AM/PM. Would be really helpful if someone can help me find a way out. I am using XCode 6.3 and my Device has iOS 8.3 installed in it.

Nik answered 22/5, 2015 at 10:59 Comment(1)
https://mcmap.net/q/216485/-am-pm-time-to-24-hour-format try this in swift 3Rhyner
F
20

Check your device settings for 24 hour time. If it is on, turn it off.

Your code is perfectly fine. I am getting output as shown below : Today's Time: 04:31:58 PM

If you don't want to show seconds use this code :

[formatter setDateFormat:@"hh:mm a"];
Fabrianne answered 22/5, 2015 at 11:3 Comment(1)
How did i miss it! Thank you. Yes it was my device settings that created the issue. Thank you so much.Nik
P
9

HH : 24 hour format

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:locale];
[formatter setDateFormat:@"HH:mm a"];

output

 Today's Time: 16:42 PM

hh : 12 hour format

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:locale];
[formatter setDateFormat:@"hh:mm a"];

output

 Today's Time: 04:43 PM
Parian answered 22/5, 2015 at 11:11 Comment(1)
Thank you. setLocale is the key for me.Klehm
P
2

Try This

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    [dateFormatter setLocale:[[NSLocale alloc]  initWithLocaleIdentifier:@"en_US_POSIX"]];
    NSString *stringFromTime = [dateFormatter stringFromDate:[NSDate date]];
Paradisiacal answered 22/5, 2015 at 11:35 Comment(0)
P
1

Change the time format of your device time to 12 hour format instead of 24 hour format. You can not get am pm in 24 hour time format.

Today's Time: 15:46:43

Petrinapetrine answered 22/5, 2015 at 11:5 Comment(0)
U
0

Try

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSLog(@"Today's Time: %@", [formatter stringFromDate:[NSDate date]]);
Unconnected answered 22/5, 2015 at 11:1 Comment(2)
Its gives an answer with 24Hr format + AM/PM (Today's Date and Time: 16:40 PM) that's absurd. I want it in a 12Hr format. I tried @"hh:mm a" and it just returns Today's Date and Time: 16:38Nik
"HH" it will return time in 24Hr format and "hh" in 12Hr formatUnconnected
G
-1

You can easily convert Time in 24 hours format .

Try this Code it is working fine for me .

        func convertTime(_ dateAsString : String) -> String{
             let dateFormatter = DateFormatter()
             dateFormatter.dateFormat = "h:mm:ss " // "h:mm:ss a"
             let date = dateFormatter.date(from: dateAsString)

             dateFormatter.dateFormat = "HH:mm a"
             let date24 = dateFormatter.string(from: date!)
             print(date24)
             return date24
          }
Gaius answered 26/7, 2018 at 8:16 Comment(1)
Why do you have to do 2 conversions for something that can be done in one?Envision

© 2022 - 2024 — McMap. All rights reserved.