一开始感觉NSDate有很多要写的,但是真要自己细细写下,反而觉得有的太繁琐或者记不住,或者压根不如用到的时候直接百度,网上一大推基本的方法运用。所以在这里自己总结了几个方法,希望有能帮到大家
1.传入北京时间( [NSDate date]是美国时间 ),获取北京时间各个参数(年月日星期)
- (NSString*)requireBeijingTime:(NSDate*)date {
NSArray *weekdays = [NSArrayarrayWithObjects: [NSNullnull],@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",nil];
NSCalendar *greCalendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponents = [greCalendarcomponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[date dateByAddingTimeInterval:-8*3600]];
NSLog(@"%ld -- %ld -- %ld -- %ld -- %ld", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day, (long)dateComponents.minute,(long)dateComponents.hour);
NSLog(@"week== %@", weekdays[dateComponents.weekday]);
return [NSString stringWithFormat:@"%ld",(long)dateComponents.hour];
}
2.将字符串格式时间转换成NSDate
+ (NSDate*)NSStringWithS:(NSString*)string {
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate*date = [dateFormatter dateFromString:string];
returndate;
}
3.将NSDate格式转成字符串
+ (NSString*)NSDateWithD:(NSDate*)date {
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString*strDate = [dateFormatterstringFromDate:date];
returnstrDate;
}
4.获取结束时间和开始时间之间的差值
-(CGFloat)getTimeWithBeginTime:(NSDate*)beginTime andEndTime:(NSDate*)endTime{
NSTimeIntervalsecondsBetweenDates= [endTimetimeIntervalSinceDate:beginTime];
CGFloattime=secondsBetweenDates/3600;
returntime;
}
好了,感觉也没什么,最后在上个获取百度时间的,防止用户修改系统时间,产品找你茬
5.获取百度时间
- (NSDate*)requireBAIDUTIME {
NSString*urlString =@"http://www.baidu.com";
NSString* urlStringg = [urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//实例化NSMutableURLRequest,并进行参数配置
NSMutableURLRequest*request = [[NSMutableURLRequestalloc]init];
[requestsetURL:[NSURLURLWithString: urlStringg]];
[requestsetCachePolicy:NSURLRequestReloadIgnoringCacheData];
[requestsetTimeoutInterval:2];
[requestsetHTTPShouldHandleCookies:FALSE];
[requestsetHTTPMethod:@"GET"];
NSHTTPURLResponse*response;
[NSURLConnectionsendSynchronousRequest:request
returningResponse:&responseerror:nil];
//处理返回的数据
//NSString *strReturn = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
PALog(@"response is %@",response);
NSString*date = [[responseallHeaderFields]objectForKey:@"Date"];
PALog(@"获取网络的字符串......%@",date);
date = [datesubstringFromIndex:5];
date = [datesubstringToIndex:[datelength]-4];
NSDateFormatter*dMatter = [[NSDateFormatteralloc]init];
NSTimeZone*timeZone = [NSTimeZonetimeZoneWithName:@"GMT"];
[dMatter setTimeZone:timeZone];
dMatter.locale= [[NSLocalealloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[dMattersetDateFormat:@"dd MMM yyyy HH:mm:ss"];
NSDate *netDate = [[dMatter dateFromString:date]dateByAddingTimeInterval:60*60*8];
PALog(@"来自网络的最后时间%@",netDate);
returnnetDate;
}
谢谢。