Reputation: 1218
I am taking date of string having format yyyy-MM-DD from database then converting it into NSDate having format yyyy-MM-DD. And again converting back it into string format dd-MM.
But when at last I get date in string format it shows one month before date.
Here is the code I have used:
NSDateFormatter *format=[[NSDateFormatter alloc]init];
NSDateFormatter *format2=[[NSDateFormatter alloc]init];
NSString *date;
[format setDateFormat:@"yyyy-MM-DD"];
[format2 setDateFormat:@"DD-MMM"];
dict=[[NSMutableDictionary alloc]init];
for(int i=0;i<[viewHistoryData count];i++)
{
dict=[viewHistoryData objectAtIndex:i];
date=[dict objectForKey:@"Date"];
NSLog(@"My date with out format = %@",date);
NSString *dateString =[format2 stringFromDate:[format dateFromString:date]];
NSLog(@"My date is = %@",dateString);
[tempArray addObject:dateString];
}
OUTPUT
2014-02-07 15:01:07.586 VirtualRunner-V3[3580:c07] My date with out format = 2014-02-07
2014-02-07 15:01:07.588 VirtualRunner-V3[3580:c07] My date is = 07-Jan
Does anybody know how to solve this?
Upvotes: 0
Views: 543
Reputation: 17585
Correct the two format.
[format setDateFormat:@"yyyy-MM-dd"];
[format2 setDateFormat:@"dd-MMM"];
But I think, Issue related with timezone. Use below to format.
[format2 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];
Upvotes: 0
Reputation: 20410
The format DD
representes day of the year, not day of the month, so change your formatters to this:
[format setDateFormat:@"yyyy-MM-dd"];
[format2 setDateFormat:@"dd-MMM"];
Upvotes: 0
Reputation: 4660
You are using the wrong placehodler for day of month. What you are using is Day of Year ranging from 1 ... 365. So instead of
[format setDateFormat:@"yyyy-MM-DD"];
use
[format setDateFormat:@"yyyy-MM-dd"];
See this link for a complete overview.
Upvotes: 4