ios - NSDate精华操作
NSData转换NSDictionary
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//用于uidate,picker。。 +(NSDate*) convertDateFromString:(NSString*)uiDate { NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ; [formatter setDateFormat:@"yyyy年MM月dd日"]; NSDate *date=[formatter dateFromString:uiDate]; return date; } //输入的日期字符串形如:@"1992-05-21 13:08:08" - (NSDate *)dateFromString:(NSString *)dateString{ NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *destDate= [dateFormatter dateFromString:dateString]; [dateFormatter release]; return destDate; } - (NSString *)stringFromDate:(NSDate *)date{ NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; //zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; NSString *destDateString = [dateFormatter stringFromDate:date]; [dateFormatter release]; return destDateString; } - (IBAction)handleDidPressBtn1{ NSLog(@"%@/n", [selfdateFromString:@"2011-08-09 12:02:01"]); NSLog(@"%@/n", [selfstringFromDate:[selfdateFromString:@"2011-08-09 12:02:01"]]); } 输出结果如下: 2011-08-09 04:02:01 +0000 2011-08-09 12:02:01 GMT+08:00
//获取当前时区
//时区转换,取得系统时区,取得格林威治时间差秒 NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT]; int nTimezone = (int)timeZoneOffset/60.0/60.0; NSString *strTimezone = @""; if(nTimezone > 0) { strTimezone = [NSString stringWithFormat:@"GMT+%d",nTimezone]; } else { strTimezone = [NSString stringWithFormat:@"GMT%d",nTimezone]; } //设置时区 [CommonReq updateTimezone:strTimezone];
//判断12/24小时制
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"a"]; NSString *AMPMtext = [dateFormatter stringFromDate:[NSDate date]]; [dateFormatter release]; if ([AMPMtext isEqualToString:@""]){ NSLog(@"24小时制"); } else { NSLog(@"12小时制"); }
//获取NSDate - 年/月/日 : 时/分/秒
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *comps = [calendar components:unitFlags fromDate:date]; int year = [comps year]; int month = [comps month]; int day = [comps day]; int hour = [comps hour]; int min = [comps minute]; int sec = [comps second];
//获取数据异常
//获取数据有异常 NSError *error = [[NSError alloc] init]; NSData *pData = = [NSData dataWithContentsOfURL:URL options:NSDataReadingMapped error:&error]; NSLog(@"download string end error = %d", [error code]); [error release];
//比较两个时间差
double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate]; long lTime = (long)intervalTime; NSInteger iSeconds = lTime % 60; NSInteger iMinutes = (lTime / 60) % 60; NSInteger iHours = (lTime / 3600)$; NSInteger iDays = lTime/60/60/24; NSInteger iMonth = lTime/60/60/24/12; NSInteger iYears = lTime/60/60/24/384; NSLog(@"相差M年d月 或者 d日d时d分d秒", iYears,iMonth,iDays,iHours,iMinutes,iSeconds);
//日期排序
//与otherDate比较,相同返回YES - (BOOL)isEqualToDate:(NSDate *)otherDate; //与anotherDate比较,返回较早的那个日期 - (NSDate *)earlierDate:(NSDate *)anotherDate; //与anotherDate比较,返回较晚的那个日期 - (NSDate *)laterDate:(NSDate *)anotherDate; //该方法用于排序时调用 - (NSComparisonResult)compare:(NSDate *)other; //. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame //. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending //. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
//字符串,NSDate互转
NSString *dateStr=@"2013-08-13 20:28:40"; //格式化 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fromdate=[dateFormatter dateFromString:dateStr]; //时区 NSTimeZone *fromzone = [NSTimeZone systemTimeZone]; //同例1 NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate]; NSDate *fromDate = [fromdate dateByAddingTimeInterval: frominterval]; NSLog(@"fromdate=%@",fromDate); //将NSDate转为NSString NSString *strTime = [fromDate description]; //如果直接将NSDate格式化 NSDate *recv = [NSDate dateFromString:recvTime withFormat:@"yyyy-MM-dd HH:mm:ss Z"];
获取时间,计算时区,stone
NSDate *now = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: now]; NSDate *localeDate = [now dateByAddingTimeInterval: interval];
将日期转换为秒(2013-01-18转换为秒)
// 转换微博过期时间 NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSTimeZone *fromzone1 = [NSTimeZone systemTimeZone]; NSInteger frominterval1 = [fromzone1 secondsFromGMTForDate: sinaweibo.expirationDate]; NSDate *fromDate = [sinaweibo.expirationDate dateByAddingTimeInterval: frominterval1]; long curDate = [fromDate timeIntervalSince1970]*1; NSString *maxTime = [NSString stringWithFormat:@"%ld",curDate];
//当前时间转换成毫秒
//将时间戳转换为NSDate类型 -(NSDate *)getDateTimeFromMilliSeconds:(long long) miliSeconds { NSTimeInterval tempMilli = miliSeconds; NSTimeInterval seconds = tempMilli/1000.0;//这里的.0一定要加上,不然除下来的数据会被截断导致时间不一致 NSLog(@"传入的时间戳=%f",seconds); return [NSDate dateWithTimeIntervalSince1970:seconds]; } //将NSDate类型的时间转换为时间戳,从1970/1/1开始 -(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime { NSTimeInterval interval = [datetime timeIntervalSince1970]; NSLog(@"转换的时间戳=%f",interval); long long totalMilliseconds = interval*1000 ; NSLog(@"totalMilliseconds=%llu",totalMilliseconds); return totalMilliseconds; }
long long time = [self getDateTimeTOMilliSeconds:[NSDate date]]; NSLog(@"%llu",time); NSDate *dat = [self getDateTimeFromMilliSeconds:time]; NSDateFormatter * formatter = [[NSDateFormatter alloc ] init]; [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss.SSS"]; NSString *date = [formatter stringFromDate:dat]; NSString *timeLocal = [[NSString alloc] initWithFormat:@"%@", date]; NSLog(@"\n%@", timeLocal);