更改 NSDateFormatter 中一周的第一天

发布于 2024-09-16 15:47:37 字数 281 浏览 7 评论 0原文

为了获取一周中的几天,我使用:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];

Weekdays 为我提供了一个包含日期名称的数组,但它从星期日开始。由于某些原因,我希望该数组在周一或周日开始,具体取决于设备的本地化设置。

有办法做到吗?

In order to get the days of the week I use:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];

Weekdays gives me an array with the days names but it begins by Sunday. For some reasons I want this array to begin by Monday or Sunday depending on the localisation settings of the device.

Is there a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

通知家属抬走 2024-09-23 15:47:37

您可以从具有当前区域设置的 NSCalendar 对象的 -firstWeekday 方法获取当前区域设置的第一个工作日的从 1 开始的索引。然后你可以相应地修改你的周名称数组:

// get week day names array
NSArray *weekdays = self.shortWeekdaySymbols;

// adjust array depending on which weekday should be first
NSUInteger firstWeekdayIndex = [NSCalendar currentCalendar].firstWeekday - 1;
if (firstWeekdayIndex) {

    NSRange firstRange = NSMakeRange(firstWeekdayIndex, weekdays.count - firstWeekdayIndex);
    NSRange lastRange = NSMakeRange(0, firstWeekdayIndex);

    NSArray *firstArray = [weekdays subarrayWithRange:firstRange];
    NSArray *lastArray  = [weekdays subarrayWithRange:lastRange];

    weekdays = [firstArray arrayByAddingObjectsFromArray:lastArray];
}

NSLog(@"%@", weekdays);

我没有 iPhone SDK,但据我所知,这些 API 应该都可用,并且行为方式与 OS X 上相同。

You can get the 1-based index of the first weekday of the current locale from the -firstWeekday method of an NSCalendar object with the current locale. Then you can modify your week names array accordingly:

// get week day names array
NSArray *weekdays = self.shortWeekdaySymbols;

// adjust array depending on which weekday should be first
NSUInteger firstWeekdayIndex = [NSCalendar currentCalendar].firstWeekday - 1;
if (firstWeekdayIndex) {

    NSRange firstRange = NSMakeRange(firstWeekdayIndex, weekdays.count - firstWeekdayIndex);
    NSRange lastRange = NSMakeRange(0, firstWeekdayIndex);

    NSArray *firstArray = [weekdays subarrayWithRange:firstRange];
    NSArray *lastArray  = [weekdays subarrayWithRange:lastRange];

    weekdays = [firstArray arrayByAddingObjectsFromArray:lastArray];
}

NSLog(@"%@", weekdays);

I don't have the iPhone SDK but AFAIK these APIs should be all available there and behave the same way as on OS X.

喜爱皱眉﹌ 2024-09-23 15:47:37

使用 Swift 实现了类似于 hasseg 的结果:

    var symbols = dateFormatter.shortWeekdaySymbols as! [String]
    let firstDayIndex = calendar.firstWeekday - 1

    if firstDayIndex > 0 {
        var sub = symbols[0..<firstDayIndex]
        symbols.removeRange(Range<Int>(start:0, end:firstDayIndex))
        symbols += sub
    }

Achieved a result similar to hasseg in Swift using:

    var symbols = dateFormatter.shortWeekdaySymbols as! [String]
    let firstDayIndex = calendar.firstWeekday - 1

    if firstDayIndex > 0 {
        var sub = symbols[0..<firstDayIndex]
        symbols.removeRange(Range<Int>(start:0, end:firstDayIndex))
        symbols += sub
    }
不美如何 2024-09-23 15:47:37

类似于 myexec 解决方案,但更通用且同样简洁

let numDays = Calendar.current.weekdaySymbols.count
let first = Calendar.current.firstWeekday
let end = first + numDays - 1

let days = (first...end).map {Calendar.current.weekdaySymbols[$0 % numDays]}

like the myexec solution but more generic and equally concise

let numDays = Calendar.current.weekdaySymbols.count
let first = Calendar.current.firstWeekday
let end = first + numDays - 1

let days = (first...end).map {Calendar.current.weekdaySymbols[$0 % numDays]}
緦唸λ蓇 2024-09-23 15:47:37
for i in 0...6 {
    let day = Calendar.current.weekdaySymbols[(i + Calendar.current.firstWeekday - 1) % 7]
    print(day)
}
for i in 0...6 {
    let day = Calendar.current.weekdaySymbols[(i + Calendar.current.firstWeekday - 1) % 7]
    print(day)
}
尸血腥色 2024-09-23 15:47:37

您需要使用NSCalender以编程方式更改一周的开始日期。

NSCalendar *_calendar;
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
_calendar.timeZone = [NSTimeZone localTimeZone];
[_calendar setFirstWeekday:2];// Sunday == 1, Saturday == 7
_calendar.locale = [NSLocale currentLocale];

    NSDateComponents *dateComponent = [_calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]];
    NSDate *now = [NSDate date];
    NSDateComponents *comp = [_calendar components:NSCalendarUnitYear fromDate:now];
    [comp setWeekOfMonth:dateComponent.weekOfYear];  //Week number.
    [comp setWeekday:2];

    NSDate * weekstartPrev = [_calendar dateFromComponents:comp];

You need to use NSCalender to change the start day of your week from programatically.

NSCalendar *_calendar;
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
_calendar.timeZone = [NSTimeZone localTimeZone];
[_calendar setFirstWeekday:2];// Sunday == 1, Saturday == 7
_calendar.locale = [NSLocale currentLocale];

    NSDateComponents *dateComponent = [_calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]];
    NSDate *now = [NSDate date];
    NSDateComponents *comp = [_calendar components:NSCalendarUnitYear fromDate:now];
    [comp setWeekOfMonth:dateComponent.weekOfYear];  //Week number.
    [comp setWeekday:2];

    NSDate * weekstartPrev = [_calendar dateFromComponents:comp];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文