如何从年月日工作日乘以iOS中的某个整数的总和中提取日期分量?
how to extract date components from sum of year month day weekday multiply by certain integer in iOS?
苹果示例项目DateSectionTitles演示了如何按年和月对部分进行分组,如何扩展它以支持年月日和工作日?
sectionIdentifier
是从持久日期属性 timestamp
.
派生的瞬态属性
- (NSString *)sectionIdentifier {
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
/*
Sections are organized by month and year. Create the section identifier
as a string representing the number (year * 1000 + month);
this way they will be correctly ordered chronologically regardless of
the actual name of the month.
*/
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit)
fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];
[self setPrimitiveSectionIdentifier:tmp];
}
return tmp;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
/*
Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
To display the section title, convert the year and month components to a string representation.
*/
static NSDateFormatter *formatter = nil;
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM YYYY" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = year;
dateComponents.month = month;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSString *titleString = [formatter stringFromDate:date];
return titleString;
}
我不知道像下面的代码那样计算年月日和工作日的确切算法。
tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];
以及数据源方法的计算。
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
您需要一个 "algorithm" 来为每一天创建不同的结果(工作日与一天相同。没有 "Thursday, 13 March 2015")。
假设您有 2015 年 3 月 13 日。一个好的唯一字符串是 20150313。或 year * 10000 + month * 100 + day
。
例如:
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", [components year] * 10000 + [components month] * 100 + [components day]];
然后反转计算以取回各个组件:
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 10000;
NSInteger month = (numericSection - (year * 10000))/100;
NSInteger day = (numericSection - (year * 10000 + month * 100))
或者只是从以下部分的对象之一获取 NSDate:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection;// = [[self.fetchedResultsController sections] objectAtIndex:section];
static NSDateFormatter *formatter = nil;
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM yyyy dd EEEE" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
APLEvent *event = [[theSection objects] firstObject];
if (event) {
NSDate *date = [event timeStamp];
return [formatter stringFromDate:date];
}
return nil;
}
苹果示例项目DateSectionTitles演示了如何按年和月对部分进行分组,如何扩展它以支持年月日和工作日?
sectionIdentifier
是从持久日期属性 timestamp
.
- (NSString *)sectionIdentifier {
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
/*
Sections are organized by month and year. Create the section identifier
as a string representing the number (year * 1000 + month);
this way they will be correctly ordered chronologically regardless of
the actual name of the month.
*/
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit)
fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];
[self setPrimitiveSectionIdentifier:tmp];
}
return tmp;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
/*
Section information derives from an event's sectionIdentifier, which is a string representing the number (year * 1000) + month.
To display the section title, convert the year and month components to a string representation.
*/
static NSDateFormatter *formatter = nil;
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM YYYY" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.year = year;
dateComponents.month = month;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
NSString *titleString = [formatter stringFromDate:date];
return titleString;
}
我不知道像下面的代码那样计算年月日和工作日的确切算法。
tmp = [NSString stringWithFormat:@"%d", [components year] * 1000 + [components month]];
以及数据源方法的计算。
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 1000;
NSInteger month = numericSection - (year * 1000);
您需要一个 "algorithm" 来为每一天创建不同的结果(工作日与一天相同。没有 "Thursday, 13 March 2015")。
假设您有 2015 年 3 月 13 日。一个好的唯一字符串是 20150313。或 year * 10000 + month * 100 + day
。
例如:
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:[self timeStamp]];
tmp = [NSString stringWithFormat:@"%d", [components year] * 10000 + [components month] * 100 + [components day]];
然后反转计算以取回各个组件:
NSInteger numericSection = [[theSection name] integerValue];
NSInteger year = numericSection / 10000;
NSInteger month = (numericSection - (year * 10000))/100;
NSInteger day = (numericSection - (year * 10000 + month * 100))
或者只是从以下部分的对象之一获取 NSDate:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> theSection;// = [[self.fetchedResultsController sections] objectAtIndex:section];
static NSDateFormatter *formatter = nil;
if (!formatter)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
NSString *formatTemplate = [NSDateFormatter dateFormatFromTemplate:@"MMMM yyyy dd EEEE" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:formatTemplate];
}
APLEvent *event = [[theSection objects] firstObject];
if (event) {
NSDate *date = [event timeStamp];
return [formatter stringFromDate:date];
}
return nil;
}