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

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"]

Related

Correct Format for String to Date?

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.

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

Date Formatting/Error handling trouble in iOS

I have an iOS app that receives data via webservice calls. Everything was working then, without warning, the app began to crash. I have traced it down to a line that formats date-time values. The webservice originally returned date values as:
yyyy-MM-dd'T'HH:mm:ss.sssz
then, without warning the date format changed to:
yyyy-MM-dd'T'HH:mm:ss
and an exception resulted. I have corrected the problem, however, the web service provides data from thousands of stations around the nation that report time sensitive data. Therefore, I can't guarantee that the old date format isn't still returned from some web service calls.
Coming from a C# and Java background, my normal approach to this issue would be a try/catch block. However, Objective-C is a different animal when it comes to exception handling. I'm not sure how to handle the different date formats. Here is my current code to do the date-time formatting
-(NSString*)stringDateFormatter:(NSString*)dateToFormat{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
//[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
[df setTimeZone:[NSTimeZone localTimeZone]];
NSDate *newDateValue = [df dateFromString:dateToFormat];
[df setDateFormat:#"HH:mm"];
NSString *retValue = [df stringFromDate:newDateValue];
return retValue;
}
What might be a best practice in hadling the two different date-time formats? Thanks! V
If you might get a date string in one of two different formats then try one first. If you get a nil result, try the other format.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *newDateValue = [df dateFromString:dateToFormat];
if (!newDateValue) {
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
newDateValue = [df dateFromString:dateToFormat];
}
BTW - there is no need to set the timezone to the local timezone. That is already the default.

NSDate to String conversion and display in the UILabel

I am trying to get a string from NSDate object type and display it in a label.
Here is the code I am using,
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *datey = [dateFormatter dateFromString:selectedDate];
NSString *labelData = [dateFormatter stringFromDate:datey];
dateLabel.Text = labelData;
where selectedDate is a String containing date in yyyy-mm-dd format.
The label is not showing up the date. But if I try giving it a string like #"someblah", it is displaying. Where am I going wrong??
Help appreciated. Thanks
Your line dateLabel.Text should be dateLabel.text.
So you start with a date string, convert it to a date, and then convert it back to a string using the same formatter. Isn't it easier to use:
dateLabel.text = selectedDate;
In all likelihood you are trying to create an NSDate using a date string that is inconsistent with the format. For example, if the format is #"yyyy-MM-dd" but your selectedDate differs from that format then the formatter won't return a NSDate. You can avoid this by setting the DateFormat to be correct for the selectedDate and then, once parsed, change the DateFormat for the desired output. Or use two NSDateFormatter instances.
Try specifying a specific format instead of NSDateFormatLongStyle, for example
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
I found this answer in 2 seconds using google.. (Yes thats a hint)

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