Can't convert String containing CET Time Zone to NSDate - ios

So, I have the following Date string that commes from a Server:
2020-05-09 18:33:00 CET
So, in order to get a NSDate from that NSString I use the following code:
+(NSDate*) converFirebaseStringDateToDate:(NSString*)dateStr {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateStr];
return dateFromString;
}
But this doesn't seem to be working, always the return dateFromString is NIL (as if the date format was invalid) which is not according to the documentation.. What am I missing here? This very same peace of code is working just fine with string like: 2020-05-09 16:26:33 GMT or even 2020-05-09 12:35:02 EDT
Also been trying to use 2020-05-09 18:33:00 CEST as Input but still failing!

Related

dateformatter producing wrong result on converting datefromstring

My string is strDate:04/12/2016 on converting it to date in format dd/MM/yyyy. It is producing another format.it is producing result as weekEndDate:2016-12-04 00:00:00 +0000. My code is
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
weekEndDate=[formatter dateFromString:strDate];
2016-12-04 00:00:00 +0000
This is not a wrong format, whenever you will print an object of NSDate in your debugger that object will be printed like the above string i.e. in UTC timezone, if you want to check correct format then convert this date object in string and print that string you will get correct format.
It's normal that you get that format, if you want to get your weekend date using that formatter here is how you do it :
NSString *strDate = #"04/12/2016";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *weekEndDate = [formatter dateFromString:strDate];
NSLog(#"%#", [formatter stringFromDate:weekEndDate]); // 04/12/2016
NSLog(#"%#", weekEndDate); // 2016-12-04 00:00:00 +0000

Converting UTC string into NSDate

I have a date-time string returned by the server in UTC format: 2015-04-21T00:54:46.469Z
I am trying to convert this into NSDate. The code is:
NSString *dateString = #"2015-04-21T00:54:46.469Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-DDThh:mm:ss.sZ"];
NSDate *locationDate = [dateFormatter dateFromString:dateString];
The value of locationDate is nil after executing this
You should use
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
or
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Any way, T must be in quotes. It seems to be a bug in Apple.

NSDateFormatter issue when date string convert into NSDate object

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE,d MMM YYYY"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"Sun,2 Nov 2014"];
output date from string giving wrong output like 2013-12-21 18:30:00 +0000
Let me guess, you are doing this to test the output:
NSLog(#"%#", dateFromString);
instead do:
NSLog(#"%#", [dateFormatter stringFromDate:dateFromString]);
This is because NSLog() will call [NSDate description] to format the date and that won't account for any formatting or time zone you may want.

Convert NSString to NSDate, adding time

I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35

Not able to convert NSString into NSDate properly

I am having trouble with date formatter. I am setting date format and passing the date string in the appropriate way (i think). But the result log shows some other dateand the GMT value has been lost. What am I doing wrong here ? Can anyone help me out here ?
NSDateFormatter *newFormatter = [[[NSDateFormatter alloc] init] autorelease];
[newFormatter setDateFormat:#"yyyy-mm-dd hh:mm:ss zzz"];
NSDate *lObjDate = [newFormatter dateFromString:#"2012-11-05 01:45:03 GMT+05:30"];
NSLog(#">>>>> %#",lObjDate);
>>>>> 2012-01-04 20:15:03 +0000
You may try:
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];

Resources