2

I am trying to convert NSString to NSdate. The string has date in it.i use the following

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(@"Date : %@",date);

the input string contains the date in the format 02/08/2011 when i log date , i get 2011-02-07 18:30:00 GMT,

I want to get the date as 02/08/2011 only. Where am i going wrong

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
pradeep
  • 3,005
  • 12
  • 42
  • 65
  • Is there any problem with iphone sdk? yesterday also i saw a similar question http://stackoverflow.com/questions/4954155/nsdateformatter-datefromstring-for-24hr-date-gives-wrong-time/4954269#4954269 – KingofBliss Feb 11 '11 at 06:45

3 Answers3

5

In your code, you are asking the date formatter to create a date object for you from a given string. Then you printed out that date object. What you want is to create that date object, then ask the date formatter to format that date object you just created. You should be calling stringFromDate instead.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(@"Date: %@", [dateFormat stringFromDate:date]);
kailoon
  • 2,131
  • 1
  • 18
  • 33
0

You will not be able to change the date representation. These (NSDateFormatter, NSCalendar) classes are provided to get formatted strings not date.

Whenever you will have an instance of NSDate class it will be in same format like you are getting.

2011-02-07 18:30:00 GMT

If you want custom styles better you go with NSString

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
-1

You are printing the actual date object which doesn't follow the date format you specified. You could do something like

[[NSDate dateWithString:setitempricedate] stringFromDate];
raidfive
  • 6,603
  • 1
  • 35
  • 32