IOS NSDate

81 篇文章 1 订阅
12 篇文章 0 订阅
+(NSString*)GetDetailTime{
    //获得系统时间
    NSDate * senddate=[NSDate date];

    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    //[dateformatter setLocale:[NSLocale localeWithLocaleIdentifier:@""]];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * morelocationString=[dateformatter stringFromDate:senddate];
    //    NSArray*array=[morelocationString componentsSeparatedByString:@"-"];
    return morelocationString;
}
//计算当前时间往后推days天
-(NSDate *)dateAfterDays:(int)days{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;

    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];

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

    return [calendar dateFromComponents:components];
}
//获取网络服务器时间
+(void)getNetTimeWithCompleteHandle:(void(^)(BOOL isSuccess,NSDate* date))complete{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString * urlstr = @"https://www.baidu.com";
    NSURL *url = [NSURL URLWithString:urlstr];
    NSMutableURLRequest *muRequest = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2.0f];
    muRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:muRequest completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
               //}
        NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
        //if ([response respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *dict=[httpResponse allHeaderFields];
        NSString *time=[dict objectForKey:@"Date"];
        NSString *timeStr=[time substringWithRange:NSMakeRange(5, 20)];//截取字符串
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
        NSLocale *local=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
        [formatter setLocale:local];//需要配置区域,不然会造成模拟器正常,真机日期为null的情况
        [formatter setTimeZone:timeZone];
        [formatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];//设置源时间字符串的格式
        NSDate* date = [formatter dateFromString:timeStr];//将源时间字符串转化为NSDate
        long timesecond = date.timeIntervalSince1970;

        if (error==nil) {
            complete(YES,date);
        }else{
            complete(NO,date);
        }
    }];
    [task resume];
}
//计算两个时间差是几天
NSDateFormatter *formater= [[ NSDateFormatter alloc]init];
    [formater setDateFormat:@"yy/MM/dd"];
    NSDate *  expireD = [formater dateFromString:expireDate];

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

    NSDateComponents *comps = [gregorian components:NSCalendarUnitEra fromDate:expireD1  toDate:expireD  options:0];
    NSInteger days = [comps day];
    NSLog(@"%ld天",(long)days);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值