Create NSDate for today at a particular hour and minute

后端 未结 1 1404
耶瑟儿~
耶瑟儿~ 2021-01-01 06:40

How can I create an NSDate with today\'s date and an hour, minutes, and seconds of 5, 0, and 0 respectively? I.e. the date will be 07/02/2010 05:00:00

相关标签:
1条回答
  • 2021-01-01 07:21
    //Gives us the current date
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:[NSDate date]];
    [components setHour:5];
    NSDate * date = [gregorian dateFromComponents:components];
    [gregorian release];
    

    Something along those lines should do the trick.

    0 讨论(0)
提交回复
热议问题