NSCalendar 日期周数
NSCalendar week number of date
我正在尝试从日期中获取周数。谁能告诉我为什么不管我输入什么日期都只返回 0:
let currentWeekNumber = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekOfYear, fromDate: myDate).weekOfYear
myDate 的格式是这样的 2015-01-23 11:52:35 +0000
Swift 中的解决方案会很好 :)
看来你贴出的行没有错误。只需检查以下解决方案以确认结果是否相同。
注意: 只是试图消除任何与日期无关的问题的可能性。
对于 Swift 你可以使用。
let calender = NSCalendar.currentCalendar()
let dateComponent = calender.components(NSCalendarUnit.WeekOfYearCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.YearCalendarUnit, fromDate:NSDate())
println ("Week: = \(dateComponent.weekOfYear)")
因此响应应该如下所示:
Week: = 4
使用此代码
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth fromDate:[NSDate date]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger month = [components month];
NSInteger year = [components year];
NSInteger day = [components day];
NSLog(@"%ld",(long)[components weekOfMonth]);
试试这个:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth fromDate:[NSDate date]];
NSInteger curr_month = [components month];
NSInteger curr_year = [components year];
NSInteger curr_date = [components day];
NSLog(@"%d %d %d, \n Week no: %ld",curr_date, curr_month,curr_year, (long)[components weekOfMonth]);
输出为:2015-01-22 18:19:40.209 样本[38734:3031191] 22 1 2015,
第 4 周
我正在尝试从日期中获取周数。谁能告诉我为什么不管我输入什么日期都只返回 0:
let currentWeekNumber = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekOfYear, fromDate: myDate).weekOfYear
myDate 的格式是这样的 2015-01-23 11:52:35 +0000
Swift 中的解决方案会很好 :)
看来你贴出的行没有错误。只需检查以下解决方案以确认结果是否相同。
注意: 只是试图消除任何与日期无关的问题的可能性。 对于 Swift 你可以使用。
let calender = NSCalendar.currentCalendar()
let dateComponent = calender.components(NSCalendarUnit.WeekOfYearCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.YearCalendarUnit, fromDate:NSDate())
println ("Week: = \(dateComponent.weekOfYear)")
因此响应应该如下所示:
Week: = 4
使用此代码
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth fromDate:[NSDate date]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger month = [components month];
NSInteger year = [components year];
NSInteger day = [components day];
NSLog(@"%ld",(long)[components weekOfMonth]);
试试这个:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekOfMonth fromDate:[NSDate date]];
NSInteger curr_month = [components month];
NSInteger curr_year = [components year];
NSInteger curr_date = [components day];
NSLog(@"%d %d %d, \n Week no: %ld",curr_date, curr_month,curr_year, (long)[components weekOfMonth]);
输出为:2015-01-22 18:19:40.209 样本[38734:3031191] 22 1 2015,
第 4 周