NSDate formatter Problem its getting null while converting NSString to NSDate - objective-c

hi to all
I am Struggling to convert NSString to NSDate with the Help of formatter but its going to be null
Can any one help please
here is my code...
NSString *dateString=[NSString stringWithFormat:#"10/26/2010/09:56:56 PM"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"mm/dd/yyyy HH:mm:ss a"];
NSDate *localDateTime =[dateFormat dateFromString:dateString];
NSLog(#"client date is %#",localDateTime);
Thx in Advance

Could be because your dateString and format does not match.
10/26/2010/09:56:56 PM
mm/dd/yyyy HH:mm:ss a
Do you see the difference? :) They actually have to match. In the format you have a space between the year and hours and in the actual date there is a slash. Month you match by 'mm' which is actually minutes and should be 'MM'. Change to:
10/26/2010 09:56:56 PM
MM/dd/yyyy HH:mm:ss a
There might be more problems so I suggest you actually look at the guide to get it right.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
NSDate *formatterDate = [dateFormatter dateFromString:#"1999-07-11 at 10:30"];
[dateFormatter release];
Try this out.

Related

NSDateFormatter not working to covert NSString to NSDate

I am using NSDateFormatter to get a date from an NSString I am confused as to why the following is not working:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: timeDateUpdated];
myDate remains nil and yet, as the image shows the timeDateUpdated appears to be of the correct format.
Wrong format, use HH instead of hh.
The h directive is for hour in the am/pm format, will not work with 17.
See this comprehensive date formatting guide

NSDateFormatter gets stringFromDate wrong

I have a string as follows:
2012-11-01
And I want to get a string representing the day from that. So this is how I approach it:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-mm-dd"];
NSDate *date = [dateFormatter dateFromString: #"2012-11-01"];
[dateFormatter setDateFormat: #"EEE"];
NSString *string = [dateFormatter stringFromDate: date];
NSLog(#"%#", string);
And the output is:
Sun
When it really should be:
Thu
(If my calculations are correct!)
So what is the problem here? Is this an internal bug? Something wrong with my code?
Thanks!
Change your first date format string to:
[dateFormatter setDateFormat: #"yyyy-MM-dd"]; // Capital MM for month
It should work then.

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

NSDateFormatter date from string correct format

I need to know how to get my string: 16/03/2012 1:15 PM
into an NSDate to add to calendar.
What would be the correct format for that?
Cheers!
You need to convert a NSString to NSDate:
NSString* myStringDate = #"16/03/2012 1:15 PM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSDate *resultDate = [df dateFromString: myStringDate];

Convert RSS date format to german date in Xcode

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.