您的当前位置:首页正文

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

2022-06-15 来源:知库网

//获取当地时间

- (NSString*)getCurrentTime {

        NSDateFormatter *formatter = [[NSDateFormatteralloc] init];

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

        NSString *dateTime = [formatter stringFromDate:[NSDatedate]];

        returndateTime;

}

//将字符串转成NSDate类型

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

        NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

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

        NSDate *destDate= [dateFormatter dateFromString:dateString];

        return destDate;

}

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

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

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

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

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

        NSDate *beginningOfWeek = [gregorian dateFromComponents:components];

        NSDateFormatter *dateday = [[NSDateFormatteralloc] init]; 

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

        return[dateday stringFromDate:beginningOfWeek];

}

显示全文