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]]; }

TNAHosting($5/月)4核/12GB/500GB/15TB/芝加哥机房

TNAHosting是一家成立于2012年的国外主机商,提供VPS主机及独立服务器租用等业务,其中VPS主机基于OpenVZ和KVM架构,数据中心在美国芝加哥机房。目前,商家在LET推出芝加哥机房大硬盘高配VPS套餐,再次刷新了价格底线,基于OpenVZ架构,12GB内存,500GB大硬盘,支持月付仅5美元起。下面列出这款VPS主机配置信息。CPU:4 cores内存:12GB硬盘:500GB月流...

Linode($5/月),新用户注册送100美元,11个数据中心云服务器

关于Linode,这是一家运营超过18年的VPS云主机商家,产品支持随时删除(按小时计费),可选包括美国、英国、新加坡、日本、印度、加拿大、德国等全球十多个数据中心,最低每月费用5美元($0.0075/小时)起。目前,注册Linode的新用户添加付款方式后可以获得100美元赠送,有效期为60天,让更多新朋友可以体验Linode的产品和服务。Linode的云主机产品分为几类,下面分别列出几款套餐配置...

racknerd新上架“洛杉矶”VPS$29/年,3.8G内存/3核/58gSSD/5T流量

racknerd发表了2021年美国独立日的促销费用便宜的vps,两种便宜的美国vps位于洛杉矶multacom室,访问了1Gbps的带宽,采用了solusvm管理,硬盘是SSDraid10...近两年来,racknerd的声誉不断积累,服务器的稳定性和售后服务。官方网站:https://www.racknerd.com多种加密数字货币、信用卡、PayPal、支付宝、银联、webmoney,可以付...

nstimeinterval为你推荐
vds是什么场效应管的工作原理是什么?徐正溪独孤天下杨勇是谁演的 独孤天下杨勇历史原型是谁个人域名申请个人怎么申请网站域名公有云平台私有云办公平台是什么组建云服务器微信小程序的搭建需要多大云服务器上海云盾上海东北人开的债务清欠公司都是骗子公司,不停做广告骗人骗钱。卡巴斯基下载卡巴斯基破解器下载修改host文件win10如何修改host文件linux虚机VMware 下的linux虚拟机操作系统下载魔兽世界服务器推荐WOW找服务器,大家给推荐几个?
怎么注册域名 tk域名注册 网站备案域名查询 cn域名个人注册 分销主机 美国主机推荐 香港托管 缓存服务器 账号泄露 淘宝双十一2018 css样式大全 免费个人空间申请 谁的qq空间最好看 刀片式服务器 双十一秒杀 阿里校园 如何注册阿里云邮箱 登陆空间 外贸空间 web应用服务器 更多