iOS中NSDate常用转换操作整合

原文出处http://blog.csdn.net/feng2qing/article/details/54974128

//当前时间格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss+ (NSString*)getCurrentDataWithDateFormate:(NSString*)formate

{NSDate*now = [NSDatedate];return[selfdateFormattingWithDate:now toFormate:formate];

}//任意NSDate格式化+ (NSString*)dateFormattingWithDate:(NSDate*)date toFormate:(NSString*)formate

{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:formate];return[formatter stringFromDate:date];

}//获取当天0点时间+ (NSDate*)returnToDay0Clock

{NSDate*now = [NSDatedate];NSCalendar*calender = [NSCalendarcurrentCalendar];

NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];inthour = (int)[dateComponent hour];intminute = (int)[dateComponent minute];intsecond = (int)[dateComponent second];//当前时分秒:hour,minute,second//返回当前时间(hour * 3600 + minute * 60 + second)之前的时间,即为今天凌晨0点NSDate*nowDay = [NSDatedateWithTimeIntervalSinceNow: - (hour *3600+ minute *60+ second)];longlonginter = [nowDay timeIntervalSince1970] *1000;NSDate*newDate = [NSDatedateWithTimeIntervalSince1970:inter /1000];returnnewDate;

}//获取当天24点时间+ (NSDate*)returnToDay24Clock

{NSDate*now = [NSDatedate];NSCalendar*calender = [NSCalendarcurrentCalendar];

NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];inthour = (int)[dateComponent hour];intminute = (int)[dateComponent minute];intsecond = (int)[dateComponent second];//一天是60分钟 * 60秒 * 24小时 = 86400秒NSDate*nextDay = [NSDatedateWithTimeIntervalSinceNow: - (hour *3600+ minute *60+ second) +86400];returnnextDay;

}//获取当前秒数+ (longlong)getCurrentDateSecond

{return[[NSDatedate] timeIntervalSince1970];

}//NSDate转秒+ (longlong)dateTosecond:(NSDate*)date

{return[date timeIntervalSince1970];

}//秒转NSDate+ (NSDate*)secondToDate:(longlong)second

{return[NSDatedateWithTimeIntervalSince1970:second];

}//是否是12小时制; YES:12小时制 / NO:24小时制+ (BOOL)is12HourSystem

{NSString*formatStringForHour = [NSDateFormatter dateFormatFromTemplate:@"j"options:0locale:[NSLocale autoupdatingCurrentLocale]];NSRangecontains = [formatStringForHour rangeOfString:@"a"];BOOLis12HourSystem = contains.location!=NSNotFound;returnis12HourSystem;

}//朋友圈/聊天 时间显示样式+ (NSString*)dateDisplayResult:(longlong)secondCount

{NSDate*date = [selfsecondToDate:secondCount];NSCalendar*calender = [NSCalendarcurrentCalendar];//判断是否是今天if([calender isDateInToday:date]) {longlongdateSecondCount = [[NSDatedate] timeIntervalSinceDate:date];if(dateSecondCount <60) {return@"刚刚";

}if(dateSecondCount < (60*60)) {return[NSStringstringWithFormat:@"%d分钟前",(int)(dateSecondCount /60)];

}return[NSStringstringWithFormat:@"%d小时前",(int)(dateSecondCount / (60*60))];

}//判断是否是昨天NSString*formatterString = @" HH:mm";if([calender isDateInYesterday:date]) {

formatterString = [@"昨天"stringByAppendingString:formatterString];

}else{//判断是否是一年内formatterString = [@"MM-dd"stringByAppendingString:formatterString];//判断是否值一年之前NSDateComponents *component = [calender components:NSCalendarUnitYear fromDate:date toDate:[NSDatedate] options:NSCalendarWrapComponents];if(component.year>=1) {

formatterString = [@"YYYY-"stringByAppendingString:formatterString];

}

}

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:formatterString];

formatter.locale= [NSLocale localeWithLocaleIdentifier:@"en"];return[formatter stringFromDate:date];

}//比较两个NsDate对象的时间差+ (CompareResult *)compareDateDifferenceDate1:(NSDate*)date1 date2:(NSDate*)date2

{

CompareResult *result = [[CompareResult alloc] init];

result.value= (fabs([date1 timeIntervalSinceDate:date2]));

result.trend= [date1 compare:date2];returnresult;

}

推荐阅读更多精彩内容