iOS-計算兩個日期之間的天數

//計算兩個日期之間的天數

+ (NSInteger) calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate

{

//創建日期格式化對象

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

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];


//取兩個日期對象的時間間隔:

//這裏的NSTimeInterval 並不是對象,是基本型,其實是double類型,是由c定義的:typedef double NSTimeInterval;

NSTimeInterval time=[endDate timeIntervalSinceDate:beginDate];


int days=((int)time)/(3600*24);

//int hours=((int)time)%(3600*24)/3600;

//NSString *dateContent=[[NSString alloc] initWithFormat:@"%i%i小時",days,hours];

    return days;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章