如何仅将 UIDatePicker 的日期添加到标签中 - 例如仅添加“28”当选择的日期是“2011年10月28日”时

发布于 2024-12-09 06:00:56 字数 253 浏览 1 评论 0原文

可能的重复:
UIDatePicker 和 NSDate

我可以这样做吗?如何做?我只需要获取用户输入的日期并将其放入标签中,而不是整个日期(例如旧:28/10/2011 新:28)

谢谢

Possible Duplicate:
UIDatePicker and NSDate

Can I do this and how? I simply need to get the day inputted by the user and put it into a label, rather than the whole date (e.g. old: 28/10/2011 new: 28)

Thanks

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

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

发布评论

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

评论(2

明天过后 2024-12-16 06:00:56

如果您的用户输入是文本,您将需要从 NSDateFormatter 开始,然后您可以从日历组件中提取日期。 (我建议通过 UIDatePicker 收集日期)

//Start here for a date that is a string
NSString *userinput = @"28/10/2011";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date = [formatter dateFromString:userinput];
[formatter release];

//Start here if you already have an `NSDate` object
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit 
                                                               fromDate:date];

NSString *dayForLabel = [NSString stringWithFormat:@"%d", [components day]];

If your user input is text you will need to start with an NSDateFormatter and then you can extract the day from the calendar components. (I recommend collecting the date through a UIDatePicker)

//Start here for a date that is a string
NSString *userinput = @"28/10/2011";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSDate *date = [formatter dateFromString:userinput];
[formatter release];

//Start here if you already have an `NSDate` object
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit 
                                                               fromDate:date];

NSString *dayForLabel = [NSString stringWithFormat:@"%d", [components day]];
静谧 2024-12-16 06:00:56

像这样的事情:

NSDate *date = [picker date];

NSCalendar *calendar = [picker calendar];

NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];

NSInteger day = [dateComponents day];

label.text = [NSString stringWithFormat:@"%d", day];

Something like this:

NSDate *date = [picker date];

NSCalendar *calendar = [picker calendar];

NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];

NSInteger day = [dateComponents day];

label.text = [NSString stringWithFormat:@"%d", day];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文