nstimeinterval如何把时间转换为几天前,几小时前这种格式

nstimeinterval  时间:2021-05-27  阅读:()

怎么获取时间差距问题

NSDate *start = [NSDate date];// do stuff...NSTimeInterval timeInterval = [start timeIntervalSinceNow];

如何在iPhone项目中获取两个时间值的差值呢?

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];[dateFormatter setDateFormat:@"mm:ss:SS"];NSDate* firstDate = [dateFormatter dateFromString:@"01:00:00"];NSDate* secondDate = [dateFormatter dateFromString:@"01:02:00"];NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

怎么设定一个时间并转换成当前时间的timeInterval

- (int)intervalSinceNow: (NSString *) theDate   {   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 dateWithTimeIntervalSinceNow:0];   NSTimeInterval now=[dat timeIntervalSince1970]*1;   NSString *timeString=@"";   NSTimeInterval cha=now-late;   if (cha/86400>1)   {   timeString = [NSString stringWithFormat:@"%f", cha/86400];   timeString = [timeString substringToIndex:timeString.length-7];   return [timeString intValue];   }   return -1;   }   上面的例子只是计算相差了几天   当然可以计算其他数值   cha/3600<1 分钟   if (cha/3600>1&&cha/86400<1) 小时   01   // 获取当前日期   02   NSDate *date = [NSDate date];   03   04   // 打印结果: 当前时间 date = 2013-08-16 09:00:04 +0000   05   NSLog(@"当前时间 date = %@",date);   06   07   // 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可   08   date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];   09   10   //打印结果:当前时间 往后60s的时间date = 2013-08-16 09:01:04 +0000   11   NSLog(@"当前时间 往后60s的时间date = %@",date);   PS:测试时时间是下午5点,但是得到的当前时间却是上午9点,相差了8小时,是时区的问题   解决办法:   1   NSTimeZone *zone = [NSTimeZone systemTimeZone];   2   3   NSInteger interval = [zone secondsFromGMTForDate: date];   4   5   NSDate *localDate = [date dateByAddingTimeInterval: interval];   6   7   // 打印结果 正确当前时间 localDate = 2013-08-16 17:01:04 +0000   8   NSLog(@"正确当前时间 localDate = %@",localDate);

ios中获取当前时间减去一段时间怎么做

//得到当前的时间 NSDate * date = [NSDate date]; NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //设置时间间隔(秒)(这个我是计算出来的,不知道有没有简便的方法 ) NSTimeInterval time = 365 * 24 * 60 * 60;//一年的秒数 //得到一年之前的当前时间(-:表示向前的时间间隔(即去年),如果没有,则表示向后的时间间隔(即明年)) NSDate * lastYear = [date dateByAddingTimeInterval:-time]; //转化为字符串 NSString * startDate = [dateFormatter stringFromDate:lastYear];

如何把时间转换为几天前,几小时前这种格式

NSDate * d = [yourformatter dateFromString:theDate]; NSTimeInterval late = [d timeIntervalSince1970]*1; NSString * timeString = nil; NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval now = [dat timeIntervalSince1970]*1; NSTimeInterval cha = now - late; if (cha/3600 < 1) { timeString = [NSString stringWithFormat:@"%f", cha/60]; timeString = [timeString substringToIndex:timeString.length-7]; int num= [timeString intValue]; if (num <= 1) { timeString = [NSString stringWithFormat:@"刚刚..."]; }else{ 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]; ...NSDate * d = [yourformatter dateFromString:theDate]; NSTimeInterval late = [d timeIntervalSince1970]*1; NSString * timeString = nil; NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval now = [dat timeIntervalSince1970]*1; NSTimeInterval cha = now - late; if (cha/3600 < 1) { timeString = [NSString stringWithFormat:@"%f", cha/60]; timeString = [timeString substringToIndex:timeString.length-7]; int num= [timeString intValue]; if (num <= 1) { timeString = [NSString stringWithFormat:@"刚刚..."]; }else{ 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]; int num = [timeString intValue]; if (num < 2) { timeString = [NSString stringWithFormat:@"昨天"]; }else if(num == 2){ timeString = [NSString stringWithFormat:@"前天"]; }else if (num > 2 && num <7){ timeString = [NSString stringWithFormat:@"%@天前", timeString]; }else if (num >= 7 && num <= 10) { timeString = [NSString stringWithFormat:@"1周前"]; }else if(num > 10){ timeString = [NSString stringWithFormat:@"n天前"]; } } 上述好像有个弊端,忘记了,对于最近的时间,可以用下面的判断 NSTimeInterval secondPerDay = 24*60*60; NSDate * yesterDay = [NSDate dateWithTimeIntervalSinceNow:-secondPerDay]; NSCalendar * calendar = [NSCalendar currentCalendar]; unsigned uintFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents * souretime = [ponents:uintFlags fromDate:d]; NSDateComponents * yesterday = [ponents:uintFlags fromDate:yesterDay]; if (souretime.year == yesterday.year && souretime.month == yesterday.month && souretime.day == yesterday.day){ [yourformatter setDateFormat:@"HH:mm"]; timeString = [NSString stringWithFormat:@" 昨天 %@ ",[self.hourformatter stringFromDate:d]]; }

火数云 55元/月BGP限时三折,独立服务器及站群限时8折,新乡、安徽、香港、美国

火数云怎么样?火数云主要提供数据中心基础服务、互联网业务解决方案,及专属服务器租用、云服务器、专属服务器托管、带宽租用等产品和服务。火数云提供洛阳、新乡、安徽、香港、美国等地骨干级机房优质资源,包括BGP国际多线网络,CN2点对点直连带宽以及国际顶尖品牌硬件。专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松快捷运用云计算!多年云计算领域服务经...

百纵科技:美国独立服务器租用/高配置;E52670/32G内存/512G SSD/4IP/50M带宽,999元/月

百纵科技怎么样?百纵科技国人商家,ISP ICP 电信增值许可证的正规公司,近期上线美国C3机房洛杉矶独立服务器,大带宽/高配置多ip站群服务器。百纵科技拥有专业技术售后团队,机器支持自动化,自助安装系统 重启,开机交付时间 30分钟内交付!美国洛杉矶高防服务器配置特点: 硬件配置高 线路稳定 洛杉矶C3机房等级T4 平价销售,支持免费测试,美国独服适合做站,满意付款。点击进入:百纵科技官方网站地...

VPSMS:53元/月KVM-512MB/15G SSD/1TB/洛杉矶CN2 GIA

VPSMS最近在做两周年活动,加上双十一也不久了,商家针对美国洛杉矶CN2 GIA线路VPS主机提供月付6.8折,季付6.2折优惠码,同时活动期间充值800元送150元。这是一家由港人和国人合资开办的VPS主机商,提供基于KVM架构的VPS主机,美国洛杉矶安畅的机器,线路方面电信联通CN2 GIA,移动直连,国内访问速度不错。下面分享几款VPS主机配置信息。CPU:1core内存:512MB硬盘:...

nstimeinterval为你推荐
独立服务器租用价格我想做网站,问了一下都要好几千,做一个独立服务器的网站真的这么贵dota启动项steam上的能不能像dota一样设置启动项进国服dota启动项dota2国服启动项怎么设置?求教香港主机哪个好路过香港,想买台游戏主机。请问下香港哪里买比较好?还有就是现在买的话,ps3 ps4 Xbox36阿里云服务器怎么重装系统怎么重装系统?日本名字大全日本女生名字大全?免费服务器资源免费资源赞助 免费赞助服务器 带宽 www.22w.com在线制作透明图片怎么样最简单做半透明图片hnd-132192168.1.132无线密码是多少施乐700施乐700代码010-342求高手指点
windows虚拟主机 免费二级域名 过期域名查询 新网域名解析 希网动态域名 息壤主机 新世界电讯 微信收钱 电信虚拟主机 服务器监测 shopex主机 重庆电信服务器托管 dnspod lamp什么意思 美国迈阿密 国外免费云空间 睿云 优惠服务器 windowsserver2012 cdn免备案空间 更多