开发者

how to get date from august 02 to september 02 [closed]

开发者_JS百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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)]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜