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

易速互联月付299元,美国独立服务器促销,加州地区,BGP直连线路,10G防御

易速互联怎么样?易速互联是国人老牌主机商家,至今已经成立9年,商家销售虚拟主机、VPS及独立服务器,目前商家针对美国加州萨克拉门托RH数据中心进行促销,线路采用BGP直连线路,自带10G防御,美国加州地区,100M带宽不限流量,月付299元起,有需要美国不限流量独立服务器的朋友可以看看。点击进入:易速互联官方网站美国独立服务器优惠套餐:RH数据中心位于美国加州、配置丰富性价比高、10G DDOS免...

ZoeCloud:香港BGP云服务器,1GB内存/20GB SSD空间/2TB流量/500Mbps/KVM,32元/月

zoecloud怎么样?zoecloud是一家国人商家,5月成立,暂时主要提供香港BGP KVM VPS,线路为AS41378,并有首发永久8折优惠:HKBGP20OFF。目前,解锁香港区 Netflix、Youtube Premium ,但不保证一直解锁,谢绝以不是原生 IP 理由退款。不保证中国大陆连接速度,建议移动中转使用,配合广州移动食用效果更佳。点击进入:zoecloud官方网站地址zo...

hostodo:2021美国独立日7折优惠促销NVMe硬盘的VPS,低至$13/年,还送DirectAdmin授权

7月4日是美国独立日,大致就是国庆节的意思吧。hostodo今年提前搞了个VPS大促销活动,4款便宜VPS,相当于7折,续费不涨价,本次促销不定时,不知道有多少货,卖完为止。VPS基于KVM虚拟,NVMe阵列,1Gbps带宽,自带一个IPv4+/64 IPv6,solusvm管理,送收费版DirectAdmin授权,VPS在用就有效! 官方网站:https://www.hostodo.com ...

nstimeinterval为你推荐
阿里云企业邮箱登录阿里云企业邮箱网页版可以登录,但是outlook上面无法登陆服务器防护产品服务器安全加固产品有哪些?技术路线有什么不同?站群是什么意思什么叫网站站群cdn有什么用国内有哪些靠谱的 Javascript 库 CDN可用那好上海哪里好找工作?cdn是什么意思阿里流量包是什么意思域名分类域名如何分类?公有云平台Skinod天诺AIoT云平台是属于公有云还是私有云?硬盘hddBIOS里的设置第一启动选项里硬盘有HDD—0 ,HDD—1 ,HDD—2都怎么区分呀好看的div样式几种常见的DIV边框样式
韩国虚拟主机 如何注册网站域名 私服服务器 vmsnap3 老左博客 patcha php探针 win8.1企业版升级win10 阿里云浏览器 免费防火墙 服务器合租 ca187 申请免费空间和域名 彩虹云 免费外链相册 免费asp空间申请 双线空间 云服务是什么意思 97rb cdn服务 更多