我怎样才能有UITableview
日期的标题?
我知道我可以使用以下方法编辑标题:
self.title=@"meals for ..";
我想添加一周的日期。我怎样才能做到这一点?
我怎样才能有UITableview
日期的标题?
我知道我可以使用以下方法编辑标题:
self.title=@"meals for ..";
我想添加一周的日期。我怎样才能做到这一点?
你可以得到这样的日期:
NSDate *theDateToday = [NSDate date];
并像这样格式化:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE MMM dd"];
NSString *theDateAsAString = [dateFormatter stringFromDate:theDateToday];
将其与您所拥有的相结合:
self.title = [NSString stringWithFormat:@"meals for %@", theDateAsAString];
至于查找星期几,您可以尝试调整这个答案。
- (NSString *)getWeekStartDateAsString:(NSDate *)date {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week
[components setDay:([components day] - ((dayofweek) - 2))];// for beginning of the week.
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"yyyy-MM-dd"];
dateString2Prev = [dateFormat stringFromDate:beginningOfWeek];
weekstartPrev = [[dateFormat_first dateFromString:dateString2Prev] retain];
return weekstartPrev;
}
- (NSString *)getWeekEndDateAsString:(NSDate *)date {
NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week
[componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week
NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
[dateFormat_End setDateFormat:@"yyyy-MM-dd"];
dateEndPrev = [dateFormat stringFromDate:EndOfWeek];
weekEndPrev = [[dateFormat_End dateFromString:dateEndPrev] retain];
return weekEndPrev;
}
您的最终结果可能如下所示:
self.title = [NSString stringWithFormat:@"meals for %@-%@", [self getWeekStartDateAsString:theDateToday], [self getWeekEndDateAsString:theDateToday]];
一种鲜为人知的 NSCalendar 方法将是 IMO 的最佳选择
rangeOfUnit:startDate:interval:forDate:
. 它为您提供某个时间单位的开始和持续时间(间隔)。有了它,很容易在使用的日历中找到一周的开始,并添加 range-1 以获得该周最新的第二个。
localizedStringFromDate:dateStyle:timeStyle:
使用在 OS X v10.6、iOS 4.0 中也很少见的可用功能,很容易创建日期的本地化表示。
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfTheWeek
interval:&interval
forDate:now];
//startOfWeek is 2013-06-02 22:00:00 +0000 now (note: it is GMT with timezone support for my timezone Europe/Berlin/DST)
endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
// 2013-06-09 21:59:59 +0000
NSString *text = [NSDateFormatter localizedStringFromDate:startOfTheWeek
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterNoStyle];
text = [text stringByAppendingString:@" to "];
text = [text stringByAppendingFormat:@"%@", [NSDateFormatter localizedStringFromDate:endOfWeek
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterNoStyle]];
self.title=text;
结果03.06.13 to 09.06.13
适用于我的德语语言环境,星期一是一周的开始。