iOS获取当前时间和获取明天的日期

时间:2021-12-25 09:47:24

@property (weak, nonatomic) IBOutlet UILabel *jintian_time;

@property (weak, nonatomic) IBOutlet UILabel *mingtian_time;

self.jintian_time  :今天时间

self.mingtian_time :明天时间

self.jintian_time.text  = [publicViewController  getCurrentTime];


self.mingtian_time.text = [publicViewController  GetTomorrowDay:[publicViewController dateFromString:[publicViewController getCurrentTime]]];


//获取当地时间

+ (NSString *)getCurrentTime {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd"];

    NSString *dateTime = [formatter stringFromDate:[NSDate date]];

    return dateTime;

}

//将字符串转成NSDate类型

+ (NSDate *)dateFromString:(NSString *)dateString {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat: @"yyyy-MM-dd"];

    NSDate *destDate= [dateFormatter dateFromString:dateString];

    return destDate;

}

//传入今天的时间,返回明天的时间

+ (NSString *)GetTomorrowDay:(NSDate *)aDate {

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];

    [components setDay:([components day]+1)];

    

    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];

    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];

    [dateday setDateFormat:@"yyyy-MM-dd"];

    return [dateday stringFromDate:beginningOfWeek];

}