Show AM/PM in capitals in swift
You can set your DateFormatter amSymbol and pmSymbol as follow:
Xcode 8.3 • Swift 3.1
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
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"
How to print the time with AM/PM format in iPhone?
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]);
How to display AM/PM on text in swiftUI?
You can set amSymbol
and pmSymbol
on DateFormatter
and use the a
symbol to print it.
lazy var timeFormat: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "h:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
return formatter
}()
In my locale (US), even without setting amSymbol
and pmSymbol
, they appear as "AM"/"PM", but I'm not sure that's true across all locales.
Date not convert in AM/PM Format in swift
set your dateFormatter locale to en-US then try to convert, it's works for me when iPhone's time format is 24 hour:
let date = Date()
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en-US")
formatter.dateFormat = "hh:mm a"
let time12 = formatter.string(from: date)
print(time12)
output:
01:37 PM
Displaying AM and PM in lower case after date formatting
Unfortunately the standard formatting methods don't let you do that. Nor does Joda. I think you're going to have to process your formatted date by a simple post-format replace.
String str = oldstr.replace("AM", "am").replace("PM","pm");
You could use the replaceAll()
method that uses regepxs, but I think the above is perhaps sufficient. I'm not doing a blanket toLowerCase()
since that could screw up formatting if you change the format string in the future to contain (say) month names or similar.
EDIT: James Jithin's solution looks a lot better, and the proper way to do this (as noted in the comments)
Get AM/PM for a date time in lowercase using only a datetime format
I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower:
string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mm") +
item.PostedOn.ToString("tt").ToLower();
Another option (which I'll investigate in a sec) is to grab the current DateTimeFormatInfo, create a copy, and set the am/pm designators to the lower case version. Then use that format info for the normal formatting. You'd want to cache the DateTimeFormatInfo, obviously...
EDIT: Despite my comment, I've written the caching bit anyway. It probably won't be faster than the code above (as it involves a lock and a dictionary lookup) but it does make the calling code simpler:
string formatted = item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());
Here's a complete program to demonstrate:
using System;
using System.Collections.Generic;
using System.Globalization;
public class Test
{
static void Main()
{
Console.WriteLine(DateTime.Now.ToString("dddd, MMMM d, yyyy a\\t h:mmtt",
GetLowerCaseInfo());
}
private static readonly Dictionary<DateTimeFormatInfo,DateTimeFormatInfo> cache =
new Dictionary<DateTimeFormatInfo,DateTimeFormatInfo>();
private static object cacheLock = new object();
public static DateTimeFormatInfo GetLowerCaseInfo()
{
DateTimeFormatInfo current = CultureInfo.CurrentCulture.DateTimeFormat;
lock (cacheLock)
{
DateTimeFormatInfo ret;
if (!cache.TryGetValue(current, out ret))
{
ret = (DateTimeFormatInfo) current.Clone();
ret.AMDesignator = ret.AMDesignator.ToLower();
ret.PMDesignator = ret.PMDesignator.ToLower();
cache[current] = ret;
}
return ret;
}
}
}
How to get the time in am/pm format iphone NSDateFormatter?
If your time format is in 24hrs
please use "HH:mm:ss"
format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"
NSString *dats1 = @"16:00:00";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(@"date1 : %@", date1); **// Here returning (null)**
[dateFormatter setDateFormat:@"hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
Thanks.
Date Format in Swift
You have to declare 2 different NSDateFormatters
, the first to convert the string to a NSDate
and the second to print the date in your format.
Try this code:
let dateFormatterGet = NSDateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = NSDateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let date: NSDate? = dateFormatterGet.dateFromString("2016-02-29 12:24:26")
print(dateFormatterPrint.stringFromDate(date!))
Swift 3 and higher:
From Swift 3 NSDate
class has been changed to Date
and NSDateFormatter
to DateFormatter
.
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}
Related Topics
How to Document the Parameters of a Function'S Closure Parameter in Swift 3
Swift5 Macos Imageresize Memory Issue
Swift: Print Decimal Precision of Division
A Swift Protocol Requirement That Can Only Be Satisfied by Using a Final Class
Is There a Zip Function to Create Tuples with More Than 2 Elements
Is There a Difference Between Swift 2.0 Protocol Extensions and Java/C# Abstract Classes
How to Connect to Self Signed Servers Using Alamofire 1.3
Swift Equivalent to _Attribute((Objc_Requires_Super))
What's the Difference Between Using Aranchor to Insert a Node and Directly Insert a Node
Nsdateformatter Return Wrong Date + Swift
Adding 3D Object to Argeoanchor
How to Load an Image from Documents Directory on MACos Swift
Uiapplication.Shared.Delegate Equivalent for Scenedelegate Xcode11
What's the Difference Between Aranchor and Anchorentity