博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS NSDate
阅读量:2345 次
发布时间:2019-05-10

本文共 2944 字,大约阅读时间需要 9 分钟。

+(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);

转载地址:http://vlnvb.baihongyu.com/

你可能感兴趣的文章
metronic 1.5
查看>>
unity3d 4 assert store
查看>>
tab bar control 注意事项
查看>>
sql优化部分总结
查看>>
IDEA运行时动态加载页面
查看>>
UML总结(对九种图的认识和如何使用Rational Rose 画图)
查看>>
js遍历输出map
查看>>
easeui分页
查看>>
20个非常有用的Java程序片段
查看>>
Enterprise Architect使用教程
查看>>
Enterprise Architect 生成项目类图
查看>>
浅入深出 MySQL 中事务的实现
查看>>
UML总结(对九种图的认识和如何使用Rational Rose 画图)
查看>>
Java中使用HttpRequest获取用户真实IP地址端口
查看>>
easyUI下拉列表点击事件的使用
查看>>
js遍历map
查看>>
单例模式
查看>>
JDBC连接数据库核心代码
查看>>
java生成随机汉字
查看>>
Java反射的基本应用
查看>>