Correct Format for String to Date? - ios

I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.
This is the String i receive:
2014-12-22T06:49:40+0000
And this is how i m trying to format it and get it to a NSDate:
NSString *time = #"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];
Unfortunately myDate is always nil.
Am i missing somtheing or is my formatting simply wrong?
Thanks in advance

Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"

You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.

Related

Can't get date from NSDateFormatter with date 2012-11-21 03:57:39-04

I get a date 2012-11-21 03:57:39-04 and I can't get an NSDate from it. I use the yyyy-MM-dd HH:mm:sszz format. I guess I'm stuck with the time zone part. I've tried different kinds of 'Z' and 'z' types but still can't get it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:sszz"]; // "2012-11-21 03:57:39-04"
The z-Codes doesn't fit, because they always requires minutes. Try a single X or x instead.
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
X 1 -08
+0530
Z The ISO8601 basic format with hours field and optional minutes field. The ISO8601 UTC indicator "Z" is used when local time offset is 0. (The same as x, plus "Z".)
Just append #"00" and you should be fine
NSString *dateStr = [#"2012-11-21 03:57:39-04" stringByAppendingString:#"00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *en_US_POSIXLocale =
[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormat setLocale:en_US_POSIXLocale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
Try this format:
[dateFormatter setDateFormat:#"yyyy-MM-dd' 'HH:mm:sszzz"];
I found the error. The format wasn't the problem because the above format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for disturbing.
answer posted by the OP Iryna Tsimokhautsava
I found the error. The format wasn't the problem because the above
format #"yyyy-MM-dd HH:mm:sszz" did actually work. Sorry for
disturbing.

stringFromDate and dateFromString sometimes return null

I simply have to format the date which comes in this format "2013:07:24 11:05:04" into this format "24-lug-2013", this is the code that i use for do this:
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyy:MM:dd hh:mm:ss"];
NSDate *d = [dtF dateFromString:self.creationDate];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:#"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
NSLog(#"%#",strDate);
This code works right, the problem is that after a few call to this method, sometimes i get a null string, but the format of the input is still the same.
For example with this string as an input "2013:07:24 11:05:04" i get this: 24-lug-2013, which is exactly what i want, but after 7 call in this case, with this input "2013:07:24 13:47:31" i get a null NSDate and consequentially the result string in null.
where is the problem??
Thanks
Date formatters return nil when the date string that is passed into them does not match the format. The code you've posted uses dateFromString on an input string. If that fails, then your NSDate "d" will be nil, and so the call to stringFromDate will likely also return nil. (Although stringFromDate might also crash if you pass in an NSDate of nil)
i solved by change this line :
[dtF setDateFormat:#"yyyy:MM:dd hh:mm:ss"];
with this:
[dtF setDateFormat:#"yyyy:MM:dd HH:mm:ss"];
this is because "hh" for hours, meaning values between 1 and 12 -- 14:00:00, eg, will fail. So If you want 24-hour format use "HH"

Parsing a date string containing a 'T' and + time zone offset

I need to check on dates for certain items. I'm reading a string from a JSON file that looks like:
"revisionDate":"2013-08-28T13:07:53+00:00"
When I try to format it
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-d HH:mm:ssZ"];
NSDate *date = [dateFormat dateFromString:revisionDateTime];
date is always nil. I think this is because of that T that's in that timestamp string. I don't know how to handle it, and there doesn't seem to be any mention how (at least that I can find), in the reference I'm using.
Any ideas on how I can handle this?
Try modifying the NSDateFormatter instance to account for the T:
[dateFormat setDateFormat:#"yyyy-MM-dd T HH:mm:ssZZZZ"];
You may also want to consider encapsulating literal string values (in this case, dashes, spaces, colons, and the T) between quotes:
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZ"]

NSDateFormatter difference

What is the difference between these two Date Formats. First one give actual time but second on give time buy adding time zone offset value.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"Date: %#",dateConverted); //
Date: 2013-12-02 12:15:43 +0000
NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter1 setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
NSDate *dateConverted1 = [dateFormatter1 dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted1);
Date: 2013-12-02 06:45:43 +0000
The problem with the 2nd format is all of the needless quotes, especially around the Z. By quoting the Z this means the Z is treated as a literal character and not the timezone format specifier.
Get rid of the quotes around the Z and both will give the same result.
The second date formatter is incorrect, the 'Z' should not be single quoted, that keeps it from being considered a format character.
Also the only single quotes that are needed are around the 'T' so that is is not considered a format character but rather a literal.
See ICU User Guide: Formatting Dates and Times

How to convert NSString to NSDate with specific format

I'm sorry if this has been asked before but I did a lot of checking and I can't seem to find an answer. :(
I have a string "2013-05-7 05:53:15 +0000" and I want to convert it to a NSDate so I can compare it with the current date and time. I can't figure out how to do this. :( I successfully converted it but it changes the look of the string and I want it to be exactly 2013-05-7 05:53:15 +0000 as that is the same format I have the other NSDate item.
Any and all help is greatly appreciated.
I am confused. ;) new to objective C. old VB / older C programmer from the 80s trying to figure this stuff out. Thank you for your help. :)
My code looks like this:
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
// [df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
df1.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *titemdate1 = [df1 dateFromString: itemdate];
what i have is two dates. one that is the last date the app was run and the other is the date of the item i'm pulling down using JSON. I want to compare the two dates (which are in string format as stated above). when I do a NSLog of the strings they look perfect but when i convert to NSDate to compare i have problems. any ideas?
Try with using below code..
-(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}
First of all you should know that if you convert the dateString to date and do NSLog of date, it returns the GMT time, which will be different from your system time in most cases. So if you are getting the string in correct format, then the date formatter is probably working fine. To check this, just convert your formatted date to string and print the output.
NSLog(#"formatted date: %#",[df1 stringFromDate:titemdate1]);
If that doesnt change anything and the output is still wrong, try changing the format of your formatter.
[df1 setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];

Resources