get current date from [NSDate date] but set the time to 10:00 am

前端 未结 5 909
小鲜肉
小鲜肉 2020-12-01 02:17

How can I reset the current date retrieved from [NSDate date] but then change the time to 10:00 in the morning.

相关标签:
5条回答
  • 2020-12-01 03:00

    I just set the timezone with Matthias Bauch answer And it worked for me. else it was adding 18:30 min more.

    let cal: NSCalendar = NSCalendar.currentCalendar()
    cal.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    let newDate: NSDate = cal.dateBySettingHour(1, minute: 0, second: 0, ofDate: NSDate(), options: NSCalendarOptions())!
    
    0 讨论(0)
  • 2020-12-01 03:03

    this nsdate used different format:

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM dd, yyyy HH:mm"];
    
    NSDate *now = [[NSDate alloc] init];
    
    NSString *dateString = [format stringFromDate:now];
    
    NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
    [inFormat setDateFormat:@"MMM dd, yyyy"];
    
    NSDate *parsed = [inFormat dateFromString:dateString]; 
    
    0 讨论(0)
  • 2020-12-01 03:04
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setHour:10];
    NSDate *date = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
    [comps release];
    

    Not tested in xcode though :)

    0 讨论(0)
  • 2020-12-01 03:05

    As with all date manipulation you have to use NSDateComponents and NSCalendar

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    [components setHour:10];
    NSDate *today10am = [calendar dateFromComponents:components];
    

    in iOS8 Apple introduced a convenience method that saves a few lines of code:

    NSDate *d = [calendar dateBySettingHour:10 minute:0 second:0 ofDate:[NSDate date] options:0];
    

    Swift:

    let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    let now: NSDate! = NSDate()
    
    let date10h = calendar.dateBySettingHour(10, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
    
    0 讨论(0)
  • 2020-12-01 03:23

    You can use this method for any minute / hour / period (aka am/pm) combination:

    - (NSDate *)todayModifiedWithHours:(NSString *)hours
                               minutes:(NSString *)minutes
                             andPeriod:(NSString *)period
    {
        NSDate *todayModified = NSDate.date;
    
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSMinuteCalendarUnit fromDate:todayModified];
    
        [components setMinute:minutes.intValue];
    
        int hour = 0;
    
        if ([period.uppercaseString isEqualToString:@"AM"]) {
    
            if (hours.intValue == 12) {
                hour = 0;
            }
            else {
                hour = hours.intValue;
            }
        }
        else if ([period.uppercaseString isEqualToString:@"PM"]) {
    
            if (hours.intValue != 12) {
                hour = hours.intValue + 12;
            }
            else {
                hour = 12;
            }
        }
        [components setHour:hour];
    
        todayModified = [calendar dateFromComponents:components];
    
        return todayModified;
    }
    

    Requested Example:

    NSDate *todayAt10AM = [self todayModifiedWithHours:@"10"
                                               minutes:@"00"
                                             andPeriod:@"am"];
    
    0 讨论(0)
提交回复
热议问题