Converting Date+Time Formats - timezone

I need to convert the following format into a new format using NSDateFormatter.
'Fri, 25 Mar 2011 10:16:00 -0700'.
I tried using "aaa, dd bbb YYYY HH:MM:SS ZHHMM" as format, but it doesn't work; it gives me a date way in the past.
I also need to convert it into the Eastern Time Zone when creating a new date.
The code I used is the following one:
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
[newDateFormatter setDateFormat:#"dd MMM YYYY HH:mm:SS"];

Let's break this string down into the various portions:
Fri, 25 Mar 2011 10:16:00 -0700 becomes:
Fri: abbreviated day of the week
25: potentially zero-padded day of the month
Mar: abbreviated month
2011: year
10: potentially zero-padded hour (in 24-hour format because there is no AM/PM)
16: potentially zero-padded minute
00: zero-padded second
-0700: time zone offset
Now let's look at the date format patterns that NSDateFormatter supports:
abbreviated day of the week: EEE
zero-padded day of the month: dd
abbreviated month: MMM
year: y
zero-padded hour (24-hour): HH
zero-padded minute: mm
zero-padded second: ss
time zone offset: ZZZ
Thus, your format string should be: #"EEE, dd MMM y HH:mm:ss ZZZ".

Related

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"

NSDate, Can't get proper date format

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

(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