iphone - 如何使用 NSCalendar 获取星期几?

标签 iphone ios xcode

我想获取星期几,最好是 1 到 7 之间的整数...这可能吗?

这就是我目前必须获取当前小时、分钟和秒的信息......

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[NSDate date]];

NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

最佳答案

你可以使用这个:

NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:myDate];

NSInteger weekday = [weekdayComponents weekday];

或者与您已有的代码集成:

NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:[NSDate date]];

NSInteger weekday = [dateComponents weekday];

这将为您提供值 1 .. 7,其中 1 是星期日。

希望有帮助。

关于iphone - 如何使用 NSCalendar 获取星期几?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11835437/

相关文章:

ios - 将两个坐标点转换为东西向和南北向的距离

Xcode:表达式类型在没有更多上下文的情况下不明确

swift - 我在 xcode 中遇到错误(没有可用的重载)

iphone - 在编辑模式下使用带有 subview /图层的自定义单元格对 UITableView 进行分组

ios - "Application tried to present modally an active controller"尽管采取了各种安全防护措施

iphone - 苹果触摸图标的正确像素尺寸是多少?

iphone - 不清楚如何从另一个 UIViewController 中正确删除 UIViewController

iphone - 从 MVC (iOS) 中的异步操作返回结果

iphone - 来自 UIView : higher than on-screen resolution? 的 Uiimage

xcode - 如何发布预加载 coreData 的应用程序?