how to get date from august 02 to september 02 [closed]
Which prints current date. the below.
is there any method which gives from date's from august 02 to september 02.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@",dateString);
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
int nrOfDays = 30;
for(int i = 0; i<=nrOfDays; i++){
[comps setDay:i];
NSDate *date = [gregorian dateFromComponents:comps];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"%@",dateString);
}
[dateFormatter release];
[gregorian release];
[comps release];
i'm not 100% sure but i think this will work. might be some syntax error because i havn't tried it, no access to xcode currently.
There is a whole section on calendrical calculations in the Date and Time Programming Guide.
The important this is to use date components rather than trying to add seconds to a day.
What if the date span was different? What if daylight saving time came in / out of operation? Such things are taken into account when manipulating NSDateComponents that are brittle if you just add seconds.
use [NSDate dateSinceTimeInterval:(NSTimeInterval)]
NSTimeInterval is in secs
so for 24hrs it will be 86400secs
jus run a for loop and create a new date by adding timeInterval
NSDate *newDate = [NSDate dateSinceTimeInterval:(NSTimeInterval)]
精彩评论