代码可能有点乱 不过应该不影响使用 这些应该基本上能满足大部分用户对于时间的处理需求
DateTimeHelper.h文件
#import <Foundation/Foundation.h>
DateTimeHelper : NSObject
//时间戳转化成字符串格式日期 YYYY-MM-dd HH:mm
+(NSString*)dateTimeFromTimesp:(double)timeStamp;//时间戳转化成字符串格式日期 YYYY-MM-dd HH:mm:ss
+(NSString*)dateTimeFromTimesp1:(double)timeStamp;//时间戳转化成字符串格式日期 YYYY-MM-dd
+(NSString*)dateTimeFromTimespEx:(double)timeStamp;//时间戳转化成字符串格式日期 YYYY年MM月dd日 ss时mm分
+(NSString*)dateTimeFromTimespNYR:(double)timeStamp;//将时间戳转化成double
+(double)dateToTimesp:(NSDate*)aDate;//获取当前日期是 几号
+(NSString*)currentDay;//相对时间 N年N月N日后的日期 -数可以表示n年前的日期
+ (NSString *)relativeTime:(NSString *)dateStr years:(NSInteger)years month:(NSInteger)month day:(NSInteger)day;//将秒处理成格式化的时间
+ (NSString *)getDateFormatWithStringSeconds:(NSString *)timerStr;//获取当前系统时间格式化 参数为一个格式YYYY-MM-dd HH:mm:ss; 返回的格式和这个保持一致
+(NSString*)getCurrentSystemTime:(NSString*)formart;//获取当前网络时间时间(中国)并转换成年月日分开 返回只根据需要自己修改
+(NSString*)getTimeSegment;//将时间格式格式化成字符串《=》nsdate 转换成需要的string
+(NSString *)getStringFromDate:(NSDate*)inputDate format:(NSString *)format;+ (NSTimeInterval)getDateWithReferenceRetureIntervalFrom1970:(NSString *)reference format:(NSString *)format;
//传入一个格式化过得字符串 返回与当前时间的间隔( 秒) reference 为 时间字符串 参数为一个reference字符串的 格式根据需要自己填写(YYYY-MM-dd HH:mm:ss);
+ (NSTimeInterval)getDateWithReferenceRetureIntervalToNow:(NSString *)reference format:(NSString *)format; //返回一个 NSTimeInterval 秒计算 刚刚 1分钟前 还是一周前.. +(NSString *)getTimeString:(NSTimeInterval)time;
DateTimeHelper.m文件
#import "DateTimeHelper.h"
#define vision [[[UIDevice currentDevice]systemVersion] integerValue]<8.0
@implementation DateTimeHelper
//时间戳转化成字符串格式日期
+(NSString*)dateTimeFromTimesp:(double)timeStamp { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp]; NSString *confromTimespStr = [formatter stringFromDate:confromTimesp]; return confromTimespStr; } +(NSString*)dateTimeFromTimesp1:(double)timeStamp { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp]; NSString *confromTimespStr = [formatter stringFromDate:confromTimesp]; return confromTimespStr; } //时间戳转化成字符串格式日期 YYYY-MM-dd +(NSString*)dateTimeFromTimespEx:(double)timeStamp { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp]; NSString *confromTimespStr = [formatter stringFromDate:confromTimesp]; return confromTimespStr; }//时间戳转化成字符串格式日期 YYYY年MM月dd日 ss时mm分
+(NSString*)dateTimeFromTimespNYR:(double)timeStamp { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy年MM月dd 23时59分"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp]; NSString *confromTimespStr = [formatter stringFromDate:confromTimesp]; return confromTimespStr; }+(double)dateToTimesp:(NSDate*)aDate
{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd"]; return [aDate timeIntervalSince1970]; // 时间转时间戳的方法: // NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[aDate timeIntervalSince1970]]; }//获取当前日期是 几号
+(NSString*)currentDay { //获取当前时间 NSDate *now = [NSDate date]; // NSLog(@"now date is: %@", now); NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now]; int day = (int)[dateComponent day]; return [NSString stringWithFormat:@"%d", day]; // int year = [dateComponent year]; // int month = [dateComponent month]; // int hour = [dateComponent hour]; // int minute = [dateComponent minute]; // int second = [dateComponent second]; } //相对时间 N年N月N日后的日期 -数可以表示n年前的日期 + (NSString *)relativeTime:(NSString *)dateStr years:(NSInteger)years month:(NSInteger)month day:(NSInteger)day{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *date1 = [dateFormatter dateFromString:dateStr]; NSLog(@"%@", date1); NSDateComponents * components = [[NSDateComponents alloc] init]; components.year = years; components.month = month; components.day = day; NSCalendar * calendar = [NSCalendar currentCalendar]; //NSDate * currentDate = [NSDate date]; NSDate * nextData = [calendar dateByAddingComponents:components toDate:date1 options:NSCalendarMatchStrictly]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString * str = [formatter stringFromDate:nextData]; NSLog(@"%@",str); return str; }//将字符串秒处理成格式化的时间
+ (NSString *)getDateFormatWithStringSeconds:(NSString *)timerStr { //转化为Double double t = [timerStr doubleValue]; //计算出距离1970的NSDatereference NSDate *date = [NSDate dateWithTimeIntervalSince1970:t]; //转化为 时间格式化字符串 NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"yyyy-MM-dd HH:mm:ss"; //转化为 时间字符串 return [df stringFromDate:date]; }//获取当前系统时间格式化过得
+(NSString*)getCurrentSystemTime:(NSString *)formart{ NSDate * senddate=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; if(formart.length==0||formart==nil){ [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; }else{ [dateformatter setDateFormat:formart]; } NSString * locationString=[dateformatter stringFromDate:senddate]; // NSLog(@"locationString:%@",locationString); return locationString; }//获取当前时间并转换成年月日分开
+(NSArray*)getTimeSegment{NSCalendar *calendar=nil;
NSInteger unitFlags=0; // if(vision){ // calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // // NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | // // NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; // }else{ calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; //} NSDate *now=[NSDate date]; NSDateComponents *comps = [[NSDateComponents alloc] init]; comps = [calendar components:unitFlags fromDate:now]; NSInteger year=[comps year]; NSInteger month = [comps month]; NSInteger day = [comps day]; NSInteger hour = [comps hour]; NSInteger min = [comps minute]; NSInteger sec = [comps second]; //返回的 数组 NSArray *ary=@[@(year),@(month),@(day),@(hour),@(min),@(sec)]; // NSLog(@"--%ld-%ld-%ld %ld:%ld:%ld",year,month,day,hour,min,sec); // return [NSString stringWithFormat:@"%ld-%ld-%ld %ld:%ld:%ld",year,month,day,hour,min,sec]; return ary; } //将时间格式格式化成字符串 +(NSString *)getStringFromDate:(NSDate*)inputDate format:(NSString *)format{ NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; // [outputFormatter setLocale:[NSLocale currentLocale]]; [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]]; if(format.length==0||format==nil){ [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; }else{ [outputFormatter setDateFormat:format]; } [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *str = [outputFormatter stringFromDate:inputDate]; //NSLog(@"testDate:%@", str); return str; }传入一个格式化过得字符串 返回 一个时间字符串与1970年的( 秒)
+ (NSTimeInterval)getDateWithReferenceRetureIntervalFrom1970:(NSString *)reference format:(NSString *)format{ //将字符串转换成时间格式 一下获取的全部是北京时间 NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]]; if(format.length==0||format==nil){ [inputFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; }else{ [inputFormatter setDateFormat:format]; } //[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.0"]; NSDate* inputDate = [inputFormatter dateFromString:reference]; // NSLog(@"date = %@",inputDate); //获取两个时间之间的间隔 //NSDate *now=[NSDate date]; NSTimeInterval secondsBetweenDates=[inputDate timeIntervalSince1970]; //[now timeIntervalSinceDate:inputDate]; //NSLog(@"秒= %lf",secondsBetweenDates); return secondsBetweenDates; }//传入一个格式化过得字符串 返回 一个时间字符串与当前时间的间隔( 秒)
+ (NSTimeInterval)getDateWithReferenceRetureIntervalToNow:(NSString *)reference format:(NSString *)format{ //将字符串转换成时间格式 一下获取的全部是北京时间 /* 传入的时间字符串 20150208010203 则[inputFormatter setDateFormat:@"yyyyMMddHHmmss"]; 传入的时间字符串 2015-02-08 01:02:03 则[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; */ NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]]; if(format.length==0||format==nil){ [inputFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; }else{ [inputFormatter setDateFormat:format]; } // [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.0"]; NSDate* inputDate = [inputFormatter dateFromString:reference]; // NSLog(@"date = %@",inputDate); //获取两个时间之间的间隔 NSDate *now=[NSDate date]; NSTimeInterval secondsBetweenDates= [now timeIntervalSinceDate:inputDate]; // NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates); return secondsBetweenDates; } //返回一个字符串 刚刚 1分钟前 ... +(NSString *)getTimeString:(NSTimeInterval)segmentTime{ NSInteger allTime=segmentTime; NSInteger year=60*60*24*365; NSInteger month = 60*60*24*30; NSInteger day = 60*60*24; NSInteger hour = 60*60; NSInteger min = 60; NSString *str=nil; if (allTime<min) { str=[NSString stringWithFormat:@"刚刚"]; }else if (allTime<hour){ str=[NSString stringWithFormat:@"%ld分之前",allTime/min];}else if (allTime<day){
str=[NSString stringWithFormat:@"%ld小时之前",allTime/hour]; }else if (allTime<month){ str=[NSString stringWithFormat:@"%ld天之前",allTime/day]; }else if (allTime<year){ str=[NSString stringWithFormat:@"%ld月之前",allTime/month]; }else{ str=[NSString stringWithFormat:@"%ld年之前",allTime/year]; } //NSLog(@"%@",str); return str; }
@end