使用特定时间初始化 NSDate 对象

发布于 2024-10-14 09:50:19 字数 266 浏览 3 评论 0原文

我有 string 格式的 time,例如 2:00。我想将其初始化为带有当前日期NSDate。 我尝试这样做,

   NSDateComponents *comps = [[NSDateComponents alloc] init];
   [comps setHour:2];

但无法将其与日期对象进行比较。请帮忙

I have time in string format for example 2:00. I want to initialize it to NSDate with present date.
I tried doing

   NSDateComponents *comps = [[NSDateComponents alloc] init];
   [comps setHour:2];

I am not able to compare this with a date object. Please help

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

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

发布评论

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

评论(5

只怪假的太真实 2024-10-21 09:50:19

尝试一下,

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];

NSCalendar *gregorian = [[NSCalendar alloc]
    initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];

您应该能够与之后的日期进行比较。

也许你的意思是这样的。我认为将使用今天的日期进行设置,然后您可以从中创建 NSDateComponents 并设置时间值。

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
[weekdayComponents setHour:12];
[weekdayComponents setMinute:10];
[weekdayComponents setSecond:30];

Try

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:6];
[comps setMonth:5];
[comps setYear:2004];

NSCalendar *gregorian = [[NSCalendar alloc]
    initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];

You should be able to compare with date after that.

Maybe you mean something like. I think will set using today's date and then you can create NSDateComponents from that and set time values.

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
[weekdayComponents setHour:12];
[weekdayComponents setMinute:10];
[weekdayComponents setSecond:30];
顾冷 2024-10-21 09:50:19

要使用当前日期 2 初始化 NSDate,您必须将 NSDate 与组件年、月和日分开,例如

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian
                                       components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                       fromDate:today];

将小时设置为 2

weekdayComponents.hour = 2;

然后通过这些组件创建一个新的 NSDate

NSDate *todayAt2 = [gregorian dateFromComponents:weekdayComponents];

To initialize an NSDate with the present date at 2, you'll have to split an NSDate with components year, month, and day, e.g.

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian
                                       components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                       fromDate:today];

Set the hours to 2

weekdayComponents.hour = 2;

And then create a new NSDate by those components

NSDate *todayAt2 = [gregorian dateFromComponents:weekdayComponents];
情徒 2024-10-21 09:50:19

您需要使用 NSCalendar 对象将 NSDateComponents 转换为 NSDate。请参阅 Apple 的文档 从组件创建日期

You need to use an NSCalendar object to convert NSDateComponents to an NSDate. See Apple's documentation on Creating a Date from Components.

<逆流佳人身旁 2024-10-21 09:50:19
  func initializeDateWithTime(date:NSDate,hrs:Int,minutes:Int) -> NSDate{
    let today = date
     let gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
     let dateComponents = gregorian.components([.Year, .Month, .Day], fromDate: today)

     dateComponents.hour = hrs;
     dateComponents.minute = minute;


     let todayAtX = gregorian.dateFromComponents(dateComponents)
     return todayAtX!
}
  func initializeDateWithTime(date:NSDate,hrs:Int,minutes:Int) -> NSDate{
    let today = date
     let gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
     let dateComponents = gregorian.components([.Year, .Month, .Day], fromDate: today)

     dateComponents.hour = hrs;
     dateComponents.minute = minute;


     let todayAtX = gregorian.dateFromComponents(dateComponents)
     return todayAtX!
}
第七度阳光i 2024-10-21 09:50:19

只是将 Objective C 的一些答案汇集在一起​​,我(现在)有了这个辅助函数:

+ (NSDate*)dateForTodayAtHour:(NSUInteger)hour andMinutes:(NSUInteger)minutes {
    NSDate* today = [NSDate date];
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents* weekdayComponents = [gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:today];
    [weekdayComponents setHour:hour];
    [weekdayComponents setMinute:minutes];
    [weekdayComponents setSecond:0];
    NSDate* result = [gregorian dateFromComponents:weekdayComponents];
    return result;
}

Just bringing together some of the Objective C answers, I (now) have this helper function:

+ (NSDate*)dateForTodayAtHour:(NSUInteger)hour andMinutes:(NSUInteger)minutes {
    NSDate* today = [NSDate date];
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents* weekdayComponents = [gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:today];
    [weekdayComponents setHour:hour];
    [weekdayComponents setMinute:minutes];
    [weekdayComponents setSecond:0];
    NSDate* result = [gregorian dateFromComponents:weekdayComponents];
    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文