Initialize a NSDate object with a specific time

后端 未结 5 1894
一整个雨季
一整个雨季 2021-02-14 11:17

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

相关标签:
5条回答
  • 2021-02-14 11:33

    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];
    
    0 讨论(0)
  • 2021-02-14 11:34

    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;
    }
    
    0 讨论(0)
  • 2021-02-14 11:36

    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];
    
    0 讨论(0)
  • 2021-02-14 11:49

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

    0 讨论(0)
  • 2021-02-14 11:51
      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!
    }
    
    0 讨论(0)
提交回复
热议问题