美文网首页
ios 获取时间相关

ios 获取时间相关

作者: GrayMantis | 来源:发表于2018-01-19 13:05 被阅读0次

    //获取当地时间

    - (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];
    }
    

    相关文章

      网友评论

          本文标题:ios 获取时间相关

          本文链接:https://www.haomeiwen.com/subject/cmuooxtx.html