NSDate, Can't get proper date format - ios

This is the timestamp I am trying to get my app to read: Thu, 03 Mar 2016 16:06:42 GMT
This is the closest I have, but haven't had any success:
[df setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss z"];
EDIT: My current format prints-to/reads: Tue, 02 M02 2016 11:36:57 GMT-5
is there anything I'm missing?
Final Edit:
I should have just posted all my code...
[df setLocale:[NSLocale systemLocale]];
This was messing things up for me.

I believe your year is incorrect. Change your dateformat to
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
See documentation on formatter dates here

I tried to use your date format and convert it to a nsdate and back to a string. It should work with the format string you used.
let time = "Thu, 03 Mar 2016 16:06:42 GMT"
let dateformat = "EEE, dd MMM yyyy HH:mm:ss z"
var dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateformat
dateformatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
var date = dateformatter.dateFromString(time)
var newTime = dateformatter.stringFromDate(date!)
// newTime = "Thu, 03 Mar 2016 16:06:42 GMT"
Edit: I changed the dateformat to use lowercase ys for the year as suggested by Devster101. This is the correct format according to this spezification which is used since iOS 7

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"

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"

(Objective C) NSDate is nil to convert a date in String

I have this string date:
Wed, 14 Oct 2015 9:58 am CDT
And I'm try to convert this String to NSData with this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLL YYYY HH:mm a Z"];
NSDate *date = [dateFormat dateFromString:#"Wed, 14 Oct 2015 9:58 am CDT"];
But date, which is a NSDate variable is always nil. I have tried to change my DateFormat with different syntax but I have the same result.
The issue is you are using capital Z instead of lowercase z for the time zone abbreviation. I also suggest reducing EEE to one E, changing LLL to MMM, using yyyy for year and h instead of HH for the hour.
"E, d MMM yyyy h:mm a z"
As a quick hint to date formatting issues, try converting a date to a string first and comparing the output.

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.

Java Simple Date Formatter parsing error

How to parse "Tue Jun 05 00:00:00 GMT+05:30 2012" text to Date in Java?
I have used format "EEE MMM dd HH:mm:ss z yyyy" But not working.
The trick that you may have missed is that you must set the locale because "Tue" or "Jun" can only be understood in English.
This works :
SimpleDateFormat ft = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date dt = ft.parse("Tue Jun 05 00:00:00 GMT+05:30 2012");

Resources