获取本月每个NSDate并转换为NSString

3

我需要在我的应用程序中为每个月的每一天存储一个URL,从今天开始往前。我应该怎么做呢?

目前为止,将今天的NSDate转换为NSString没有问题:

NSDate* date = [NSDate date];
NSDateFormatter* format = [[[NSDateFormatter alloc]init]autorelease];
[format setDateFormat:@"yyyyMdd"];

NSString* str = [format stringFromDate:date];

那么,比如说,如果我想要一个字符串表示17天前的日期,我该怎么做呢?

另外,我想把这些日期存储在一个字符串中,以便获取过去一个月的每一天的URL,例如www.myurl.com/pictureYEAR-MONTH-DAY.jpg。

2个回答

6

只需获取当前日期的NSDateComponents,然后从中提取天数,编写一个for循环逐渐减少天数,直到达到1为止,每次从更改了日期的组件创建一个新日期,并将其存储为字符串。

代码可能比文字更能说明问题,所以这里是代码(已测试):

NSMutableArray *dates = [[NSMutableArray alloc] init];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyy-MM-dd"]; // you can set this to whatever you like
NSDate *today = [NSDate date]; // get todays date
NSCalendar *cal = [NSCalendar currentCalendar]; // needed to work with components
NSDateComponents *components = [cal components:(NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit) fromDate:today];
NSUInteger day = [components day];
for (NSUInteger i=day; i>0; i--) {
    // loop through all days till down to 1
    [components setDay:i]; // update the day in the components
    NSDate *date = [cal dateFromComponents:components]; 
    [dates addObject:[fmt stringFromDate:date]]; // add the new date
}

for (NSString *date in dates) {
    NSString *str = [NSString stringWithFormat:@"www.myurl.com/picture/%@",date];
    NSLog(@"%@",str); // voila, your URL.
}

[fmt release];
[dates release];

5

这类似于Alex的回答,但他的只获取从本月开始到今天为止的天数。以下是如何获取本月所有日期的方法:

NSDate *today = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];

NSMutableArray *datesThisMonth = [NSMutableArray array];
NSRange rangeOfDaysThisMonth = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:today];

NSDateComponents *components = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSEraCalendarUnit) fromDate:today];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];

for (NSInteger i = rangeOfDaysThisMonth.location; i < NSMaxRange(rangeOfDaysThisMonth); ++i) {
  [components setDay:i];
  NSDate *dayInMonth = [cal dateFromComponents:components];
  [datesThisMonth addObject:dayInMonth];
}

这将会留下一个NSArray,其中包含当前月份每一天的NSDates
如果你想要一个"17天前"的日期,也可以很容易地实现:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *diff = [[NSDateComponents alloc] init];
[diff setDay:-17];

NSDate *seventeenDaysAgo = [cal dateByAddingComponents:diff toDate:today];

一旦您拥有一个NSDate,将其转换为字符串就非常简单:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];

NSDate *date = ...;
NSString *string = [formatter stringFromDate:date];

如果我想更动态地创建它呢?(不是17天前)。例如,如果我想为每个月创建数组,包含该月每天的字符串,则怎么办? - user1096112
1
@user1096112 你可以使用rangeOfUnit:inUnit:forDate:来查找当前日期中有多少个NSMonthCalendarUnitsNSYearCalendarUnit中,然后再(多或少)在我答案的for循环周围添加另一个for循环。虽然还有更多要做,但这是大致的想法。 - Dave DeLong
这个很好用。你能告诉我如何获取当前年份的所有日期吗? - Nitesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接