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

香港 1核1G 29元/月 美国1核 2G 36元/月 快云科技

快云科技: 11.11钜惠 美国云机2H5G年付148仅有40台,云服务器全场7折,香港云服务器年付388仅不到五折 公司介绍:快云科技是成立于2020年的新进主机商,持有IDC/ICP/ISP等证件资质齐全主营产品有:香港弹性云服务器,美国vps和日本vps,香港物理机,国内高防物理机以及美国日本高防物理机官网地址:www.345idc.com活动截止日期为2021年11月13日此次促销活动提供...

优林70/月,西南高防地区最低70/月

优林怎么样?优林好不好?优林 是一家国人VPS主机商,成立于2016年,主营国内外服务器产品。云服务器基于hyper-v和kvm虚拟架构,国内速度还不错。今天优林给我们带来促销的是国内西南地区高防云服务器!全部是独享带宽!续费同价!官方网站:https://www.idc857.com​地区CPU内存硬盘流量带宽防御价格购买地址德阳高防4核4g50G无限流量10M100G70元/月点击购买德阳高防...

80VPS:香港服务器月付420元;美国CN2 GIA独服月付650元;香港/日本/韩国/美国多IP站群服务器750元/月

80vps怎么样?80vps最近新上了香港服务器、美国cn2服务器,以及香港/日本/韩国/美国多ip站群服务器。80vps之前推荐的都是VPS主机内容,其实80VPS也有独立服务器业务,分布在中国香港、欧美、韩国、日本、美国等地区,可选CN2或直连优化线路。如80VPS香港独立服务器最低月付420元,美国CN2 GIA独服月付650元起,中国香港、日本、韩国、美国洛杉矶多IP站群服务器750元/月...

nstimeinterval为你推荐
一键更换ip修改IP的 有什么办法可以一键自动切换家庭电脑的IP共享虚拟主机基础版主机与VMWare虚拟机文件共享的几种方法华为云服务找回手机我华为的手机遗失了,但是没有开启查找手机位置的功能,我该如何找回?国外手机号国外的手机号是什么样的??云主机能玩游戏吗服务器上 可以玩游戏吗cdn的作用.cdn文件是什么类型的文件?作用是什么?是否是病毒?香港亚马逊官网网址香港人在网上买东西,都是在什么网站上买东西?四叶草安全四叶草有着什么秘密、、??payoneer卡官网登录递四方后台怎么绑定payoneer卡域名解析记录值填什么域名解析有哪两种方式?分别简述其解析过程。
二级域名 域名买卖 万网域名注册 未注册域名查询 西安服务器租用 windows虚机 如何注册网站域名 淘宝二级域名 buyvm highfrequency 免费主机 好看的留言 火车票抢票攻略 国外php空间 40g硬盘 免空 南通服务器 酷番云 流媒体加速 备案空间 更多