在NSDateFormatter中更改一周的第一天

时间:2022-11-05 01:42:00

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?

有办法吗?

5 个解决方案

#1


14  

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:

您可以从具有当前语言环境的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);

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.

我没有iPhone SDK但AFAIK这些API应该全部可用,并且行为与OS X上的行为相同。

#2


1  

Achieved a result similar to hasseg in Swift using:

使用以下方法在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
    }

#3


1  

like the myexec solution but more generic and equally concise

像myexec解决方案,但更通用,同样简洁

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

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

#4


1  

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

您需要使用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];

#5


0  

for i in 0...6 {
    let day = Calendar.current.weekdaySymbols[(i + Calendar.current.firstWeekday - 1) % 7]
    print(day)
}

#1


14  

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:

您可以从具有当前语言环境的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);

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.

我没有iPhone SDK但AFAIK这些API应该全部可用,并且行为与OS X上的行为相同。

#2


1  

Achieved a result similar to hasseg in Swift using:

使用以下方法在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
    }

#3


1  

like the myexec solution but more generic and equally concise

像myexec解决方案,但更通用,同样简洁

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

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

#4


1  

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

您需要使用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];

#5


0  

for i in 0...6 {
    let day = Calendar.current.weekdaySymbols[(i + Calendar.current.firstWeekday - 1) % 7]
    print(day)
}