My application used the [NSDate date] function to get the current date. His work is terminated on days other than the 1st of each month during AM . ie Follow these steps:
- Set the system date from 01 to June 2011 and the time between 00:00 and 5:00 in the morning.
- Use the following code:
NSLog(@"Current Date :: %@",[NSDate date]);
O / P is :: Current date :: 2011-05-31 19:40:21 +0000
Desired O / P: Current Date :: 2011-06-01 00: 00: 0 (i.e. set time) +0000
Also from 6 in the morning it works great.
What is the reason for this?
Actually I don’t want NSDate in string format, but what I want is an NSDate date object corresponding to the 1st date of the current month. Why am I using the following code snippet:
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit
| NSDayCalendarUnit) fromDate:[NSDatedate]];
[comp setDay:1];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
[gregorian release];
return firstDayOfMonthDate;
Since [NSDate date] returns an invalid date, the components also contain an invalid date. The above code works for all senario, except for senario, which I posted at the beginning of this thread. What should I do?
source
share