How to setTimeStyle AM/PM to lowercase using the iPhone SDK
How can I set the AM / PM time style to lowercase? I'm using the following code and according to Apple's documentation this should returned t开发者_如何学JAVAhese values as lowercase but it doesn't.
[_detailsTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
You can change the AM and PM symbols on an NSDateFormatter as follows:
[dateFormatter setAMSymbol:@"am"];
[dateFormatter setPMSymbol:@"pm"];
or in Swift:
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"
I am not sure why Apple's examples show the AM / PM in lower case, but it is easy to make the date string lower case:
NSDateFormatter *detailsTimeFormatter = [[NSDateFormatter alloc] init];
[detailsTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@",[[detailsTimeFormatter stringFromDate:[NSDate date]] lowercaseString]);
Swift 5:
You can add handy extension to date formatter:
extension DateFormatter {
static let myFormattedDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "E MM/dd/yyyy HH:mm a" //enter any format you like.
formatter.amSymbol = "am" //By default AM/PM symbols are uppercased
formatter.pmSymbol = "pm"
return formatter
}()
}
Then you would use it like this:
DateFormatter.myFormattedDate.string(from: Date()) // Here enter your date and it will return string value formatted to the format you provided with lowercased am/pm symbols.
Checkout NSDateFormatter.com to discover the perfect format.
精彩评论