UIDatePicker 和 NSDate

发布于 2024-07-16 08:24:54 字数 224 浏览 3 评论 0原文

我有一个从 UIDatepicker 获得的 NSDate。

IBOutlet UIDatePicker *dueDate;
NSDate *selectedDate = [dueDate date];

如何以 int 的形式从 dueDate 检索月份? (1 月为 1 月,2 月为 2 月,依此类推)

I have an NSDate that I get from a UIDatepicker.

IBOutlet UIDatePicker *dueDate;
NSDate *selectedDate = [dueDate date];

How do I retrieve the month from dueDate in the form of an int? (1 for Jan, 2 for Feb, etc.)

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

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

发布评论

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

评论(3

小…红帽 2024-07-23 08:24:54
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
感性不性感 2024-07-23 08:24:54
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文