創(chuàng)建類似新浪微博發(fā)表時(shí)間顯示的轉(zhuǎn)換的類方法

新浪微博的微博內(nèi)容顯示的發(fā)表時(shí)間如示例圖1所示,從服務(wù)器獲取到的微博發(fā)表時(shí)間數(shù)據(jù)其實(shí)是如示例圖2所示,這時(shí)就需要對(duì)獲取到的發(fā)表時(shí)間數(shù)據(jù)進(jìn)行重寫get方法。在進(jìn)行重寫get方法時(shí)涉及到對(duì)時(shí)間數(shù)據(jù)的轉(zhuǎn)換,轉(zhuǎn)換方法較為典型,因此可以將轉(zhuǎn)換方法封裝為繼承NSDate的一個(gè)轉(zhuǎn)換類方法,方便以后使用。

示例圖1
示例圖2

具體的創(chuàng)建過(guò)程詳見(jiàn)文章《利用Category創(chuàng)建類的方法,快速修改frame,提高編程效率

(1)NSDate+SHKExtension.h

#import<Foundation/Foundation.h>

@interface NSDate (SHKExtension)

//判斷某個(gè)時(shí)間是否為今年

- (BOOL)isThisYear;

//判斷某個(gè)時(shí)間是否為昨天

- (BOOL)isYesterday;

//判斷某個(gè)時(shí)間是否為今天

-(BOOL)isToday;

@end


(2)NSDate+SHKExtension.m

#import "NSDate+SHKExtension.h"

@implementation NSDate (SHKExtension)

-(BOOL)isThisYear{

? ? NSCalendar *calendar=[NSCalendar currentCalendar];

? ? //獲得某個(gè)時(shí)間的年月日時(shí)分秒

? ? NSDateComponents *dateCmps=[calendar components:NSCalendarUnitYear fromDate:self];

? ? NSDateComponents *nowCmps=[calendar components:NSCalendarUnitYear fromDate:[NSDate date]];

? ? return dateCmps.year==nowCmps.year;

}

//判斷某個(gè)時(shí)間是否為昨天

-(BOOL)isYesterday{

? ? NSDate *now=[NSDate date];

? ? // date ==? 2014-04-30 10:05:28 --> 2014-04-30 00:00:00

? ? // now == 2014-05-01 09:22:10 --> 2014-05-01 00:00:00

? ? NSDateFormatter *fmt=[[NSDateFormatter alloc]init];

? ? fmt.dateFormat=@"yyyy-MM-dd";

? ? // 2014-04-30

? ? NSString *dateStr=[fmt stringFromDate:self];

? ? // 2014-10-18

? ? NSString *nowStr=[fmt stringFromDate:now];

? ? // 2014-10-30 00:00:00

? ? NSDate *date=[fmt dateFromString:dateStr];

? ? // 2014-10-18 00:00:00

? ? now=[fmt dateFromString:nowStr];

? ? NSCalendar *calendar=[NSCalendar currentCalendar];

? ? NSCalendarUnit unit=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

? ? NSDateComponents *cmps=[calendar components:unit fromDate:date toDate:now options:0];

? ? return cmps.year==0&&cmps.month==0&&cmps.day==1;

}

-(BOOL)isToday{

? ? NSDate *now=[NSDate date];

? ? NSDateFormatter *fmt=[[NSDateFormatter alloc]init];

? ? fmt.dateFormat=@"yyyy-MM-dd";

? ? NSString *dateStr=[fmt stringFromDate:self];

? ? NSString *nowStr=[fmt stringFromDate:now];

? ? return [dateStr isEqualToString:nowStr];

}

@end

(3)類方法的使用示例

//重寫get方法

-(NSString *)created_at{

? ? //日期格式化類

? ? NSDateFormatter *fmt=[[NSDateFormatter alloc]init];

? ? // 如果是真機(jī)調(diào)試,轉(zhuǎn)換歐美時(shí)間,需要設(shè)置locale

? ? fmt.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];

? ? fmt.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";

? ? //微博的創(chuàng)建日期

? ? NSDate *createDate=[fmt dateFromString:_created_at];

? ? //當(dāng)前時(shí)間

? ? NSDate *now=[NSDate date];

? ? //日歷對(duì)象(比較日期之間的差距)

? ? NSCalendar *calendar=[NSCalendar currentCalendar];

? ? // NSCalendarUnit枚舉代表想獲得哪些差值

? ? NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

? ? //計(jì)算兩個(gè)日期之間的差值

? ? NSDateComponents *cmps=[calendar components:unit fromDate:createDate ? ? toDate:now options:0];

? ? if ([createDate isThisYear]) {//今年

? ? ? ? if ([createDate isYesterday]) {//昨天

? ? ? ? fmt.dateFormat=@"昨天 HH:mm";

? ? ? ? return [fmt stringFromDate:createDate];

? ? ? ? }else if([createDate isToday]){//今天

? ? ? ? ? ? if (cmps.hour>=1) {

? ? ? ? ? ? ? ? return [NSString stringWithFormat:@"%d小時(shí)前",cmps.hour];

? ? ? ? ? ? }else if (cmps.minute>=1){

? ? ? ? ? ? ? ? return [NSString stringWithFormat:@"%d分鐘前",cmps.minute];

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? return @"剛剛";

? ? ? ? ? ? }

? ? ? ? }else{//今年的其他日子

? ? ? ? ? ? fmt.dateFormat=@"MM-dd HH:mm";

? ? ? ? ? ? return [fmt stringFromDate:createDate];

? ? ? ? }

? ? }else{//非今年

? ? ? ? fmt.dateFormat=@"yyyy-MM-dd HH:mm";

? ? ? ? return [fmt stringFromDate:createDate];

? ? }

}

小結(jié)

以上方法是針對(duì)近期自學(xué)新浪微博項(xiàng)目的一點(diǎn)小小總結(jié),行筆簡(jiǎn)陋,如有錯(cuò)誤,望指正。


"彩蛋":循環(huán)著這首《Prayer In C》寫完的這篇文章,這首本來(lái)是很普通和典型的獨(dú)立音樂(lè)曲調(diào)的歌,之后被一位德國(guó)DJ Robin Schulz重新制作后引起了轟動(dòng),曾獲多國(guó)榜單冠軍單曲。


Prayer In C - 騰訊視頻



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容