一个显示日期的iOS工具类
一个显示日期的工具类
.h文件 #import <Foundation/Foundation.h> @interface TimeUtil : NSObject + (NSString*)getTimeStr1:(long long)time; +(NSString*) getTimeStrStyle1:(long long)time; + (NSString*)getTimeStr1Short:(long long)time; +(NSString*) getTimeStrStyle2:(long long)time; +(int)dayCountForMonth:(int)month andYear:(int)year; +(BOOL)isLeapYear:(int)year; @end
.m文件
#import "TimeUtil.h" @implementation TimeUtil + (NSString*)getTimeStr:(long) createdAt { // Calculate distance time string // NSString *timestamp; time_t now; time(&now); int distance = (int)difftime(now, createdAt); if (distance < 0) distance = 0; if (distance < 60) { timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "second ago" : "seconds ago"]; } else if (distance < 60 * 60) { distance = distance / 60; timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "minute ago" : "minutes ago"]; } else if (distance < 60 * 60 * 24) { distance = distance / 60 / 60; timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "hour ago" : "hours ago"]; } else if (distance < 60 * 60 * 24 * 7) { distance = distance / 60 / 60 / 24; timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "day ago" : "days ago"]; } else if (distance < 60 * 60 * 24 * 7 * 4) { distance = distance / 60 / 60 / 24 / 7; timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "week ago" : "weeks ago"]; } else { static NSDateFormatter *dateFormatter = nil; if (dateFormatter == nil) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; } NSDate *date = [NSDate dateWithTimeIntervalSince1970:createdAt]; timestamp = [dateFormatter stringFromDate:date]; } return timestamp; } + (NSString*)getTimeStr1:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; NSString * string=[NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d",[component year],[component month],[component day],[component hour],[component minute]]; return string; } + (NSString*)getTimeStr1Short:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; NSString * string=[NSString stringWithFormat:@"%04d-%02d-%02d",[component year],[component month],[component day]]; return string; } + (NSString*)getMDStr:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; NSString * string=[NSString stringWithFormat:@"%d月%d日",[component month],[component day]]; return string; } +(NSDateComponents*) getComponent:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; return component; } +(NSString*) getTimeStrStyle1:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; int year=[component year]; int month=[component month]; int day=[component day]; int hour=[component hour]; int minute=[component minute]; NSDate * today=[NSDate date]; component=[calendar components:unitFlags fromDate:today]; int t_year=[component year]; NSString*string=nil; long long now=[today timeIntervalSince1970]; long long distance=now-time; if(distance<60) string=@"刚刚"; else if(distance<60*60) string=[NSString stringWithFormat:@"%lld分钟前",distance/60]; else if(distance<60*60*24) string=[NSString stringWithFormat:@"%lld小时前",distance/60/60]; else if(distance<60*60*24*7) string=[NSString stringWithFormat:@"%lld天前",distance/60/60/24]; else if(year==t_year) string=[NSString stringWithFormat:@"%02d-%02d %d:%02d",month,day,hour,minute]; else string=[NSString stringWithFormat:@"%d-%d-%d",year,month,day]; return string; } +(NSString*) getTimeStrStyle2:(long long)time { NSDate * date=[NSDate dateWithTimeIntervalSince1970:time]; NSCalendar * calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit; NSDateComponents * component=[calendar components:unitFlags fromDate:date]; int year=[component year]; int month=[component month]; int day=[component day]; int hour=[component hour]; int minute=[component minute]; int week=[component week]; int weekday=[component weekday]; NSDate * today=[NSDate date]; component=[calendar components:unitFlags fromDate:today]; int t_year=[component year]; int t_month=[component month]; int t_day=[component day]; int t_week=[component week]; NSString*string=nil; if(year==t_year&&month==t_month&&day==t_day) { if(hour<6&&hour>=0) string=[NSString stringWithFormat:@"凌晨 %d:%02d",hour,minute]; else if(hour>=6&&hour<12) string=[NSString stringWithFormat:@"上午 %d:%02d",hour,minute]; else if(hour>=12&&hour<18) string=[NSString stringWithFormat:@"下午 %d:%02d",hour,minute]; else string=[NSString stringWithFormat:@"晚上 %d:%02d",hour,minute]; } else if(year==t_year&&week==t_week) { NSString * daystr=nil; switch (weekday) { case 1: daystr=@"日"; break; case 2: daystr=@"一"; break; case 3: daystr=@"二"; break; case 4: daystr=@"三"; break; case 5: daystr=@"四"; break; case 6: daystr=@"五"; break; case 7: daystr=@"六"; break; default: break; } string=[NSString stringWithFormat:@"周%@ %d:%02d",daystr,hour,minute]; } else if(year==t_year) string=[NSString stringWithFormat:@"%d月%d日",month,day]; else string=[NSString stringWithFormat:@"%d年%d月%d日",year,month,day]; return string; } +(int)dayCountForMonth:(int)month andYear:(int)year { if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) { return 31; }else if(month==4||month==6||month==9||month==11){ return 30; }else if([self isLeapYear:year]){ return 29; }else{ return 28; } } +(BOOL)isLeapYear:(int)year { if (year%400==0) { return YES; }else{ if (year%4==0&&year%100!=0) { return YES; }else{ return NO; } } } @end
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!