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

ZJI:韩国BGP+CN2线路服务器,国内三网访问速度优秀,8折优惠码每月实付440元起

zji怎么样?zji最近新上韩国BGP+CN2线路服务器,国内三网访问速度优秀,适用8折优惠码zji,优惠后韩国服务器最低每月440元起。zji主机支持安装Linux或者Windows操作系统,会员中心集成电源管理功能,8折优惠码为终身折扣,续费同价,全场适用。ZJI是原Wordpress圈知名主机商:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服...

鲸云10美元,香港BGPRM 1核 1G 10Mbps峰值带宽 1TB流量,江西CN2-NAT 1核 512MB内存 100M带宽 ,

WHloud Official Notice(鲸云官方通知)(鲸落 梦之终章)]WHloud RouMu Cloud Hosting若木产品线云主机-香港节点上新预售本次线路均为电信CN2 GIA+移动联通BGP,此机型为正常常规机,建站推荐。本次预售定为国庆后开通,据销售状况决定,照以往经验或有咕咕的可能性,但是大多等待时间不长。均赠送2个快照 2个备份,1个默认ipv4官方网站:https:/...

A400互联(49元/月)洛杉矶CN2 GIA+BGP、1Gbps带宽,全场独服永久5折优惠

a400互联是一家成立于2020年商家,主营美国机房的产品,包括BGP线路、CN2 GIA线路的云服务器、独立服务器、高防服务器,接入线路优质,延迟低,稳定性高,额外也还有香港云服务器业务。当前,全场服务器5折,香港VPS7折,洛杉矶VPS5折,限时促销!A400互联官网:https://a400.net/优惠活动全场独服永久5折优惠(续费同价):0722香港VPS七折优惠:0711洛杉矶VPS五...

nstimeinterval为你推荐
服务器翻译QQ邮箱服务器怎么填cdn有什么用国内有哪些靠谱的 Javascript 库 CDN可用有没有免费的服务器有没有免费的电影网站啊 ! 知道请告诉下香港亚马逊官网香港有亚马逊kindle paperwhite买吗服务器租用多少钱一月买一台能同时容纳100人在线的服务器需要多少钱?租呢?域名分类域名的基本类型有哪些?菲律宾高防服务器菲律宾高防服务器锐一网络这家公司怎么样?ms min计算机中的latency是什么意思?接收验证码接受验证码要短信费吗短信收费吗?io域名注册id域名怎么注册?
域名查询工具 已备案域名出售 arvixe linode代购 英语简历模板word 免费博客空间 ibox官网 中国智能物流骨干网 帽子云 百兆独享 jsp空间 umax120 网站在线扫描 安徽双线服务器 无限流量 yundun 宏讯 国内域名 新网dns 时间同步服务器 更多