0

I want compare two dates. I am getting device date in yyyy-dd-mm format and getting mm-dd-yyyy from webservices. I want to change device date format and get that changed date in NSDate object. Here is my code:

 NSDate *today = [[NSDate alloc] init];     
 NSLog(@"today : %@", today);
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"MM dd yy"];
 NSString *tString = [dateFormatter stringFromDate:today];
 NSLog(@"tString : %@",tString);
 NSDate *date = [dateFormatter dateFromString:tString];
 NSLog(@"date : %@",date);
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Swapnil
  • 1,858
  • 2
  • 22
  • 49

1 Answers1

0

You are saying date coming from webservice is in mm-dd-yyyy format..Then convert it into NSDate like this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSDate *convertedDate = [dateFormatter dateFromString:yourDateStringFromWeb];

You have device date already with you..Now you can compare two NSDate objects in many ways..See this and this..

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • @Krishnabhadra: I tried above one. But when I convert that into NSdata object and again print on console, the format changed from MM-dd-yyyy – Swapnil May 23 '11 at 12:40
  • Output is: today : 2011-05-23 12:38:49 +0000 – Swapnil May 23 '11 at 12:41
  • iSwap, the main thing is you can compare between two NSDate object, you dont have to make them strings.. – Krishnabhadra May 23 '11 at 12:42
  • " I want to change device date format and get that changed date in NSDate object" , NSDate doesnt have any format. Format is only when you convert it to string to display.. – Krishnabhadra May 23 '11 at 12:43
  • @iSwap, confirm this, You are getting a date from webservice (like 5-27-2011) in strings, you want to convert it to NSDate object. Then compare it with a date which you already have in device (which is a string in format yyyy-dd-mm like 2011-27-5). Is this your requirement? – Krishnabhadra May 23 '11 at 12:48