一個顯示日期的工具類

來源:互聯網
上載者:User

標籤:style   blog   檔案   2014   html   for   

一個顯示日期的工具類

.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)        [email protected]"剛剛";    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:                [email protected]"日";                break;            case 2:                [email protected]"一";                break;            case 3:                [email protected]"二";                break;            case 4:                [email protected]"三";                break;            case 5:                [email protected]"四";                break;            case 6:                [email protected]"五";                break;            case 7:                [email protected]"六";                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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.