gpt4 book ai didi

ios - NSdate 的 UIDatePicker 问题

转载 作者:行者123 更新时间:2023-11-28 18:40:21 25 4
gpt4 key购买 nike

我有一个来 self 的数据库的旧日期的 NSDate 对象。现在我需要将我的 UIDatePicker 日期设置为当前日期,但要使用我从数据库中获取的已存储 NSDate 对象的小时和分钟值。

那么如何从旧日期中提取小时和分钟并将其设置为当前 UIDatePicker NSdate。

谢谢

最佳答案

由于 NSDate 在初始化后无法更改,因此您必须重新创建它:

要获取 NSDate 的小时和分钟部分,请使用 NSCalendar:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourOriginalDateHere];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

然后你可以创建一个新的 NSDate(也使用 NSCalendar):

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]]; //[NSDate date] is the current date
[dateComponents setHour:hour];
[dateComponents setMinute:minute];
[dateComponents setSecond:second];
NSDate *newDate = [gregorian dateFromComponents:dateComponents];

关于ios - NSdate 的 UIDatePicker 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com