【问题标题】:Convert week number for a certain year to a month name将某年的周数转换为月份名称
【发布时间】:2013-08-29 13:15:21
【问题描述】:

通过 SO 进行搜索后,除了 this question 之外,我没有找到任何解决方案。我正在考虑创建一个方法来接受周数的int 和年份的int,并返回一个带有月份名称的NSString

- (NSString *)getMonthNameFromNumber:(int)weekNumber andYear:(int)year

但我找不到解决这个问题的方法。如果有人能提供建议,我会很高兴。

【问题讨论】:

标签: ios cocoa-touch nsstring nsdate


【解决方案1】:

这样就可以了

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year {
    NSDateComponents * dateComponents = [NSDateComponents new];
    dateComponents.year = year;
    dateComponents.weekOfYear = week;
    dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar
    NSDate * date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM"];
    return [formatter stringFromDate:date];
}

请注意,这取决于设备首选项中设置的当前日历。

如果这不符合您的需要,您可以提供一个NSCalendar 实例并使用它来检索日期,而不是使用currentCalendar。通过这样做,您可以配置诸如哪一天是一周的第一天等等。 NSCalendardocumentation 值得一读。

如果使用自定义日历是一种常见情况,只需将实现更改为类似

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year {
     [self monthNameForWeek:week inYear:year calendar:[NSCalendar currentCalendar]];
}

- (NSString *)monthNameForWeek:(NSUInteger)week inYear:(NSInteger)year calendar:(NSCalendar *)calendar {
    NSDateComponents * dateComponents = [NSDateComponents new];
    dateComponents.year = year;
    dateComponents.weekOfYear = week;
    dateComponents.weekday = 1; // 1 indicates the first day of the week, which depends on the calendar
    NSDate * date = [calendar dateFromComponents:dateComponents];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM"];
    return [formatter stringFromDate:date];
}

作为一个不相关的旁注,您应该避免使用 get 方法名称,除非您间接返回一个值。

【讨论】:

  • ... 它应该返回一周的第一天所在的月份的名称。如果您担心跨月的周数,您可以使用另一个日期组件提前 6 天并检查一周的最后一天。还要为不同地区略有不同的答案做好准备——在世界大部分地区,星期一是一周的开始,但在美国是星期天。当您谈论第 N 周的开始时间时,我希望 Apple 会考虑到这一点。
  • @Tommy 你可以配置一个NSCalendar 实例。添加了注释。
  • yearweekOfYear 不足以创建有效日期(默认为指定年份的 1 月 1 日)。关于weekday的使用请看我的回答。
【解决方案2】:

与日期有关的任何事情都需要使用日历。您的问题假定为公历,但我建议您将方法声明更改为:

- (NSString*)monthNameFromWeek:(NSInteger)week year:(NSInteger)year calendar:(NSCalendar*)cal;

由此可见,我们所谈论的那一天也是模棱两可的。例如(尚未检查),2015 年第 4 周可能同时包含一月和二月。哪一个是正确的?在此示例中,我们将使用工作日 1,它表示星期日(在英国公历中),我们将使用它所属的任何月份。

因此,您的代码将是:

// Set up our date components
NSDateComponents* comp = [[NSDateComponents alloc] init];
comp.year = year;
comp.weekOfYear = week;
comp.weekday = 1;

// Construct a date from components made, using the calendar
NSDate* date = [cal dateFromComponents:comp];

// Create the month string
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM"];
return [dateFormatter stringFromDate:date];

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2018-04-20
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多