Convert RSS date format to german date in Xcode - objective-c

I have a little App which shows data from an RSS feed. Because it's date format is very long and now well for being printed out I want to convert it to the German's date format. This code I have now, but it always return "nil":
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
NSString *contentDate = [dateFormat stringFromDate:date];
What's wrong with it? Thanks for answers!

RSS dates are a bit tricky.. you can grab the NSDate+InternetDateTime.h/.m from here:
https://github.com/mwaterfall/MWFeedParser
that should take care of the hard work.

Related

NSDate returning nil for the NSDateFormatter set correctly

I am getting the date in NSString format as mentioned below.
Input:datestring = 30/04/2013 04:49 PM
I am applying the format for NSDateformatter as mentioned below to convert this NSString to NSDate .
Format:dateFormat= dd/MM/yyyy hh:mm a
I still get the date conversion to Nil. Please help as I am not understanding this issue.Here is my code snippet to explain what i am doing in the utility function.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
Try This....
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormatter dateFromString:#"30/04/2013 04:49 PM"];
NSLog(#"%#",date);

Format date with NSDateFormatter

I want to format 06-06-2013 1:51 PM as Jun 06,2013.
I have tried all possible different formatting styles using NSDateFormatter but failed.
Try this
First You need to convert this string back to NSDate then again convert the NSdate to string using formatter.
NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
[dateFormatForDB setDateFormat:#"dd-MM-yyyy HH:mm a"]; //Note capital H is 4 24-hour time format
NSDate *aDate = [[[NSDate alloc] initWithTimeInterval:0 sinceDate:[dateFormatForDB dateFromString:aDateString]] autorelease];
if(aDate){
[formatter setDateFormat:#"MMM dd,yyyy"];
NSString *date = [formatter stringFromDate:aDate];
[dateFormatForDB release];
}
Try to use this format
[df setDateFormat:#"MMM dd,yyyy];
By looking at your format, i think this is what you are looking for...
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [df dateFromString:dateString];
NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"MMM dd,yyyy"];
NSString *formattedDate = [df2 stringFromDate:newDate];
This will give you the date as string in the required format. If you want time in 24hr format, replace 'hh' with 'HH'.

Time formats for NSDate

I have datetime values coming into my app from a web service. The format is as follows:
2013-03-24T09:45:00.000-04:00
I need only the time from this value. I am using NSDateFormatter and am setting the date format as follows:
[df setDateFormat:#"HH:mm:ss"];
and when I do this I get a rather cryptic runtime error. However, I have no errors when I use the following (though it doesn't satisfy my requirement):
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
Can someone help?
Try this
NSString *originalDateString = #"2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.sssz"];
NSDate *date = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [dateFormatter stringFromDate:date];
Maybe if you use date formater to retrieve a NSDate instance and from there do you own formating.
NSSTring *dateString = "2013-03-24T09:45:00.000-04:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateFromWebApp = [dateFormatter dateFromString:dateString];
Haven't tested tho but it should work.
Cheers

NSString to NSDate conversion issue

I have Date is 2012-09-25 09:33:10 +0000 , using
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *d = [dateFormatter dateFromString:sDate];
// sDate = #"2012-September-21"
But i get nil , instead of a formatter date. What can be the issue ?
Since your sDate = #"2012-September-21", it does not have time (hour minutes and seconds) in your string sDate. So you should set your format like this
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
You don't include time into string. You also need to fix the locale:
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[dateFormatter dateFromString:#"2012-September-21"];
From your question it is not clear that what is input, but i have tested with below given input it is working
NSString *dateString = #"2012-09-25 09:33:10";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date = [dateFormatter dateFromString:dateString];

NSString to NSDate returns wrong date

I try to convert my NSString to NSDate object, but NSDateFormatter returns me a strange value.
Here is code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
[dateFormat release];
date value is 2012-08-14 21:00 +0000. It is 3 hours difference between NSString value and NSDate value. I think I've missed something, but I don't know what.
This is what i use:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00:00 +0000"];
NSLog(#"\n\n DATE: %# \n\n\n", date);
The +0000 is timezone, so make sure you use your timezone, like +0400.
Edit:
If you can't change the string, you can use this code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:#"2012-08-15 00:00"];
As i knew NSDate holds Grinwich time, so if you are in Moscow time zone, everything is wright
In objective c for NSDate if you did not set the setTimeZone, NSDate will take default timezone as localTimeZone. so if you need to get the exact date which you give as NSString string format, you need to setTimeZone as UTC. Follow the sample code, I guess it will be helpful for you.
NSDateFormatter *loacalformatter=[[NSDateFormatter alloc]init];
[loacalformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *localDate =[loacalformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"localDate :%#",localDate);
NSDateFormatter *UTCformatter=[[NSDateFormatter alloc]init];
[UTCformatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[UTCformatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate =[UTCformatter dateFromString:#"2012-08-15 00:00"];
NSLog(#"UTCDate :%#",UTCDate);
UTCDate :2012-08-15 00:00 +0000 (GMT+00:00)
As suggested in the comments, if the date you receive is UTC then you need to convert it to your local timezone. Apple recommend you always use a properly configured NSDateFormatter when displaying dates, to handle localisation issues.
Here's some example code for turning an NSDate into an NSString:
NSDate *date = // initialised elsewhere
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
dateFormat.timeZone = [NSTimeZone localTimeZone];
dateFormat.timeStyle = NSDateFormatterShortStyle;
dateFormat.dateStyle = NSDateFormatterShortStyle;
dateFormat.locale = [NSLocale currentLocale];
NSString *dateAsString = [dateFormat stringFromDate:date];