您好,登錄后才能下訂單哦!
計算兩個時間差的兩個函數
計算兩個時間差的兩個函數
兩個時間之差
- (NSString *)intervalFromLastDate: (NSString *) dateString1 toTheDate:(NSString *) dateString2
{
NSArray *timeArray1=[dateString1 componentsSeparatedByString:@"."];
dateString1=[timeArray1 objectAtIndex:0];
NSArray *timeArray2=[dateString2 componentsSeparatedByString:@"."];
dateString2=[timeArray2 objectAtIndex:0];
NSLog(@"%@.....%@",dateString1,dateString2);
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d1=[date dateFromString:dateString1];
NSTimeInterval late1=[d1 timeIntervalSince1970]*1;
NSDate *d2=[date dateFromString:dateString2];
NSTimeInterval late2=[d2 timeIntervalSince1970]*1;
NSTimeInterval cha=late2-late1;
NSString *timeString=@"";
NSString *house=@"";
NSString *min=@"";
NSString *sen=@"";
sen = [NSString stringWithFormat:@"%d", (int)cha%60];
// min = [min substringToIndex:min.length-7];
// 秒
sen=[NSString stringWithFormat:@"%@", sen];
min = [NSString stringWithFormat:@"%d", (int)cha/60%60];
// min = [min substringToIndex:min.length-7];
// 分
min=[NSString stringWithFormat:@"%@", min];
// 小時
house = [NSString stringWithFormat:@"%d", (int)cha/3600];
// house = [house substringToIndex:house.length-7];
house=[NSString stringWithFormat:@"%@", house];
timeString=[NSString stringWithFormat:@"%@:%@:%@",house,min,sen];
[date release];
return timeString;
}
一個時間距現在的時間
- (NSString *)intervalSinceNow: (NSString *) theDate
{
NSArray *timeArray=[theDate componentsSeparatedByString:@"."];
theDate=[timeArray objectAtIndex:0];
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d=[date dateFromString:theDate];
NSTimeInterval late=[d timeIntervalSince1970]*1;
NSDate* dat = [NSDate date];
NSTimeInterval now=[dat timeIntervalSince1970]*1;
NSString *timeString=@"";
NSTimeInterval cha=late-now;
if (cha/3600<1) {
timeString = [NSString stringWithFormat:@"%f", cha/60];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"剩余%@分", timeString];
}
if (cha/3600>1&&cha/86400<1) {
timeString = [NSString stringWithFormat:@"%f", cha/3600];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"剩余%@小時", timeString];
}
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"剩余%@天", timeString];
}
[date release];
return timeString;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。