NSDateFormatter not formatting date correctly - ios

I'm using NSDateFormater to format dates that i get from my server.
All the formatting works correctly except of AM/PM
let f = "EEEE, MMM dd, hh:mma"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
i get:
formated date: Tuesday, Feb 23, 11:30
and it should be:
formated date: Tuesday, Feb 23, 11:30am
Am i missing something?
Solution:
My IPhones time is set to 24 hour format, i changed it to 12 and it also changed in my app.

I think it should be
let f = "EEEE, MMM dd, hh:mm a"

I just tested this and it works
let dateFormatter = NSDateFormatter()
let f = "EEEE, MMM dd, hh:mm a"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
Demo here

Related

Date formatter Mon, 03 May 2021 00:00:00 GMT

I want to convert this String to Date
Mon, 03 May 2021 00:00:00 GMT
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss 'Z'"
date = dateFormatter.date(from: dateString)
but it doesn't work, date is nil. So what is the correct format for this case?
use the date format as like EEE, dd MMM yyyy hh:mm:ss Z
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
print(date)
You need to use HH since this is 24h format and since the day and month names are in English you also need to set the locale for the formatter so it doesn't use the users current locale
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"

DateFormatter giving incorrect Result [duplicate]

This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 3 years ago.
I try to format my UTC date but the result seems very wrong
DateFormatter:
let df = DateFormatter()
df.calendar = Calendar(identifier: .iso8601)
df.dateFormat = "MMMM dd YYYY hh mm"
df.locale = Locale(identifier: "en_US_POSIX")
df.timeZone = TimeZone(identifier: "UTC")
Example 1
Date 1:
po dateStart
▿ 2018-12-31 00:00:00 +0000
- timeIntervalSinceReferenceDate : 567907200.0
Giving:
po df.string(from: dateStart)
"December 31 2019 12 00"
Example 2
Date 2:
po dateEnd
▿ 2021-01-03 23:59:59 +0000
- timeIntervalSinceReferenceDate : 631411199.0
Giving:
po df.string(from: dateEnd)
"January 03 2020 11 59"
Why does this happen and how can I get the correct Date (Year)?
You should use "MMMM dd yyyy hh mm" instead of "MMMM dd YYYY hh mm" rest of seems ok to me.

Most efficient way to create date formatter for May 23 '17, 12:30

What format i should use for getting the date with '. like May 23 '17, 12:30. Tried MMM dd ' yy hh:mm a so im getting Mar 23, yy hh:mm
Not sure what you mean with most efficient here but to get the result you want use
let formatter = DateFormatter()
formatter.dateFormat = "MMM dd ''yy, hh:mm a"

I got exception with dateFormatter with "EEE MMM dd HH:mm:ss z yyyy"

I got exception when using the following code :
var expDate : NSDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
expDate = dateFormatter.dateFromString("Sun Apr 19 10:33:18 GST 2009")
I've tried e and E instead of EEE , Z and ZZZ instead of z
but didn't work
does anyone know what's the problem?
It appears that "GST" isn't a supported abbreviation. See https://gist.github.com/norio-nomura/d4d2475d62e446f796d8 for want of an official source.

How to format NSDateFormatter for this date

How would I format a NSDateFormatter to create a date from the following format?
Sat May 16 13:45:36 2015
The part of Sat May 16 is the part that I am unsure how to do
I know the last part would be HH:mm:ss yyyy
let dateString = "Sat May 16 13:45:36 2015"
let df = NSDateFormatter()
df.dateFormat = "E MMM dd HH:mm:ss yyyy"
df.dateFromString(dateString) // "May 16, 2015, 1:45 PM"
You should take a look at this answer also.

Resources