有谁知道如果有设置一周的第一天在NSCalendar的方式,或者是有一个已经有星期一作为一周的第一天日历,而不是星期天。 我目前正在对基于围绕一个星期的工作的一个应用程序,它需要开始上周一,周日不。 我最有可能做一些工作来解决这个问题,但会有很多的角落案件。 我宁愿平台为我做的。
提前致谢
下面是一些我使用的代码。 这是周六了,所以我会希望是,平日是6,而不是7。这将意味着,周日将是7,而不是翻转至0
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:0];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *todaysDate = [gregorian components:unitFlags fromDate:[NSDate date]];
int dayOfWeek = todaysDate.weekday;
Answer 1:
编辑:在本周初前一个月开始这并不检查边缘的情况下。 一些更新的代码来覆盖这一点: https://stackoverflow.com/a/14688780/308315
在任何情况下,仍关注这一点,你需要使用
ordinalityOfUnit:inUnit:forDate:
并设置firstWeekday为2(1 ==星期日和7 ==星期六)
下面的代码:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2]; // Sunday == 1, Saturday == 7
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:[NSDate date]];
NSLog(@"Adjusted weekday ordinal: %d", adjustedWeekdayOrdinal);
请记住,对于平日序号从1开始的一周,不为零的第一天。
文档链接 。
Answer 2:
此代码构造被设置为当前周的星期一日期:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDate *beginningOfWeek = nil;
BOOL ok = [gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek
interval:NULL forDate: today];
Answer 3:
setFirstWeekday:在NSCalendar对象。 设置接收器的首个工作日的指数。
- (void)setFirstWeekday:(NSUInteger)weekday
应该做的伎俩。
Answer 4:
在我看来这个设置应该根据用户的语言环境是动态的。 因此应该使用:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
这将导致日历自动根据用户区域设置的第一个工作日。 除非您正在开发的应用程序为特定目的/用户区域设置(或希望允许用户选择这一天)。
Answer 5:
我已经这样做了。
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDateComponents *compForWeekday = [gregorian components:(NSWeekdayCalendarUnit) fromDate:today];
NSInteger weekDayAsNumber = [compForWeekday weekday]; // The week day as number but with sunday starting as 1
weekDayAsNumber = ((weekDayAsNumber + 5) % 7) + 1; // Transforming so that monday = 1 and sunday = 7
Answer 6:
我有麻烦了很多这里的答案。 。 也许这只是我。 。
下面是对我的作品的答案:
- (NSDate*)firstDayOfWeek
{
NSCalendar* cal = [[NSCalendar currentCalendar] copy];
[cal setFirstWeekday:2]; //Override locale to make week start on Monday
NSDate* startOfTheWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:self];
return startOfTheWeek;
}
- (NSDate*)lastDayOfWeek
{
NSCalendar* cal = [[NSCalendar currentCalendar] copy];
[cal setFirstWeekday:2]; //Override locale to make week start on Monday
NSDate* startOfTheWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit startDate:&startOfTheWeek interval:&interval forDate:self];
return [startOfTheWeek dateByAddingTimeInterval:interval - 1];
}
更新:
正如指出的( 其他地方由@vikingosegundo),一般最好让当地确定哪一天是一周的开始,但是在这种情况下,OP问的一周开始在周一发生,因此我们复制该系统日历,并覆盖firstWeekDay。
Answer 7:
与克里斯的回答的问题是在本周初前一个月开始边缘情况。 下面是一些比较容易的代码,它还会检查边缘情况:
// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
// Edge case where beginning of week starts in the prior month
NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
[edgeCase setMonth:2];
[edgeCase setDay:1];
[edgeCase setYear:2013];
NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeek:[components week]];
NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);
// Find Sunday for the given date
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeek:[components week]];
NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);
return [calendar dateFromComponents:components];
}
Answer 8:
我看到在其他消息的误解。 第一个周日,无论它是,有1号不是0默认情况下,星期日= 1为在“简介日期和时间编程指南可可:历法计算”:
“在公历表示星期日平日值为1”
对于周一的第一个工作日我唯一的补救方法是蛮力的条件来解决计算
NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// set to 7 if it's Sunday otherwise decrease weekday number
NSInteger weekday=[comps weekday]==1?7:[comps weekday]-1;
Answer 9:
下面还覆盖边缘的情况下,
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
[components setWeekday:2]; // 1 == Sunday, 7 == Saturday
if([[calendar dateFromComponents:components] compare: curDate] == NSOrderedDescending) // if start is later in time than end
{
[components setWeek:[components week]-1];
}
return [calendar dateFromComponents:components];
}
Answer 10:
试试这个:
NSCalendar *yourCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]
[yourCal setFirstWeekday:0];
Answer 11:
四,发现了显示使用nscalender..using下面的代码中的任何工作日名称..只要从Xcode的菜单栏打开控制台看到results.Copy在viewDidLoad方法将以下代码粘贴到获得一周的第一天的方式
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[components setDay:([components day]-([components weekday]-1))];
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek];
NSLog(@"First_date: %@", dateString_first);
输出将是:
date: 02/11/2010 :Thursday
First_date: 02/07/2010 :Sunday
因为我曾在2010年2月11日运行此程序U将得到取决于当前日期所需的输出。
同样如果u想要得到一周的第一个工作日即星期一的日期,那么只需修改一下代码:
CHANGE: [components setDay:([components day]-([components weekday]-1))];
TO [components setDay:([components day]-([components weekday]-2))];
拿到周一的日期是星期..
同样ü可以尝试通过改变整数-1,-2等找到任何七个工作日的日期...
希望UR问题的答案..
谢谢,Bonson迪亚斯
Answer 12:
我这样做的简单的方法是让星期一= 0,星期日= 6:
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSInteger dayNumStartingFromMonday = ([dateComponents weekday] - 2 + 7) % 7; //normal: Sunday is 1, Monday is 2
Answer 13:
在ISO 8601日历似乎有它的第一个工作日默认设置为周一。
Answer 14:
使用日历nextWeekend (iOS版10或更高版本)和序数 (感谢@克里斯-马克尔)。 我已经得到了周一第一周为EN_US日历。
这里是它回退到firstWeekday一个例子:
extension Calendar {
var firstWorkWeekday: Int {
guard #available(iOS 10.0, *) else{
return self.firstWeekday
}
guard let endOfWeekend = self.nextWeekend(startingAfter: Date())?.end else {
return self.firstWeekday
}
return self.ordinality(of: .weekday, in: .weekOfYear, for: endOfWeekend) ?? self.firstWeekday
}
}
Answer 15:
你可以改变.firstWeekday
日历。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.firstWeekday = 2;
然后使用rangeOfUnit:startDate:interval:forDate:
拿到第一天
NSDate *startOfWeek;
[calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&startOfWeek interval:nil forDate:[NSdate date]];
Answer 16:
...有一个已经有星期一作为一周的第一天,而不是周日的日历。
总有一天,会有的。
文章来源: NSCalendar first day of week