Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift NSDate AM and Pm Format [duplicate]

To convert date and time, I have used this:

 let formatter = NSDateFormatter()
 formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
 formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
 formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
 formatter.dateFormat = "M/dd/yyyy, h:mm"
 formatter.AMSymbol = "AM"
 formatter.PMSymbol = "PM"
 let dateString = formatter.stringFromDate(model.courseDate)
 print(dateString)

If the course date is "courseDate = "2017-01-09 01:00:00 +0000";" but I am getting like this in print: 1/09/2017, 1:00 with no any AM or PM.

How can I achieve this?

Thanks.

like image 747
Sam Avatar asked Mar 02 '17 11:03

Sam


2 Answers

As per Apple general date formatter you should use "M/dd/yyyy, hh:mm a" in your date format.

like : formatter.dateFormat = "M/dd/yyyy, hh:mm a"

Hope this will help you!

like image 91
Shivani Gor Avatar answered Oct 07 '22 01:10

Shivani Gor


according to the date formatter specs

You can retrieve a date in 24h format instead of requesting AM/PM by using HH instead of hh

so the date "21/11/2016 01:30" (pm) would be "21/11/2016 13:30".

if you do need to print the format, though, you can print it using:

a

More date fomatting details here

like image 34
CptEric Avatar answered Oct 07 '22 00:10

CptEric