给定一个时间,计算距离当前时间的时间差
2016-08-16 00:00
537 查看
NSString *bill_timeStr=@"2016-08-11 12:12:47";
NSString *str1=[self intervalSinceNow:bill_timeStr];
调用方法
- (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=fabs(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];
double x;
x=[timeString doubleValue];
double y;
y=cha-x*3600;
NSString *str1=[NSString stringWithFormat:@"%f", y/60];
str1 = [str1 substringToIndex:str1.length-7];
timeString=[NSString stringWithFormat:@"%@时%@分", timeString,str1];
}
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
double x;
x=[timeString doubleValue];
double y;
y=cha-x*86400;
NSString *str1=[NSString stringWithFormat:@"%f", y/3600];
str1 = [str1 substringToIndex:str1.length-7];
double w;
w=[str1 doubleValue];
double e;
e=y-w*3600;
NSString *str2=[NSString stringWithFormat:@"%f", e/60];
str2 = [str2 substringToIndex:str2.length-7];
timeString=[NSString stringWithFormat:@"%@天%@时%@分", timeString,str1,str2];
}
return timeString;
}
NSString *str1=[self intervalSinceNow:bill_timeStr];
调用方法
- (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=fabs(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];
double x;
x=[timeString doubleValue];
double y;
y=cha-x*3600;
NSString *str1=[NSString stringWithFormat:@"%f", y/60];
str1 = [str1 substringToIndex:str1.length-7];
timeString=[NSString stringWithFormat:@"%@时%@分", timeString,str1];
}
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
double x;
x=[timeString doubleValue];
double y;
y=cha-x*86400;
NSString *str1=[NSString stringWithFormat:@"%f", y/3600];
str1 = [str1 substringToIndex:str1.length-7];
double w;
w=[str1 doubleValue];
double e;
e=y-w*3600;
NSString *str2=[NSString stringWithFormat:@"%f", e/60];
str2 = [str2 substringToIndex:str2.length-7];
timeString=[NSString stringWithFormat:@"%@天%@时%@分", timeString,str1,str2];
}
return timeString;
}
相关文章推荐
- mysql 计算一个时间距离当前天数
- 计算距离当前时间
- IOS_计算当前时间与某时间的时间差
- iOS 获取当前时间以及计算年龄(时间差)
- iOS 计算当前时间与过去某一时间的时间差
- 由过去的某一时间,计算距离当前的时间(Java实现)
- 计算指定时间与当前的时间差 比如,3天前、10分钟前
- iOS 获取当前时间以及计算年龄(时间差)
- 计算指定时间与当前的时间差
- 计算指定时间与当前的时间差
- .Net 获取一个时间距离当前时间多久了
- 设M 是一个m×n 的矩阵,其中每行的元素从左到右单增有序,每列的元素从上到下单增有序。 给出一个分治算法计算出给定元素x 在M 中的位置或者表明x 不在M 中。分析算法的时间复杂性。
- 通过日期计算距离当前时间的距离
- 计算指定时间与当前的时间差
- .net 计算当前时间距离今晚00:00:00还有多少分多少秒
- 传入一个时间,返回和当前的时间差
- 设M 是一个m×n 的矩阵,其中每行的元素从左到右单增有序,每列的元素从上到下单增有序。 给出一个分治算法计算出给定元素x 在M 中的位置或者表明x 不在M 中。分析算法的时间复杂性。
- C#计算某个时间距离当前日期的天数
- 计算当前时间和⼀一个固定时间的差值
- 计算距离当前时间相隔的天数,小时数,分数,秒数