NSTimeInterval的使用

简介: <span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">第一种方式: </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px
第一种方式: 
NSTimeInterval time = ...; 
NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li", 
                                              lround(floor(time / 3600.)) % 100, 
                                              lround(floor(time / 60.)) % 60, 
                                              lround(floor(time.)) % 60]; 

第二种方式: 
// You could also have stored the start time using 
    // CFAbsoluteTimeGetCurrent() 
    NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow]; 

    // Divide the interval by 3600 and keep the quotient and remainder 
    div_t h = div(elapsedTime, 3600); 
    int hours = h.quot; 
    // Divide the remainder by 60; the quotient is minutes, the remainder 
    // is seconds. 
    div_t m = div(h.rem, 60); 
    int minutes = m.quot; 
    int seconds = m.rem; 

    // If you want to get the individual digits of the units, use div again 
    // with a divisor of 10. 

    NSLog(@"%d:%d:%d", hours, minutes, seconds) 

如果您有您初始日期存储在 NSDate 对象时,您可以获得新日期任何时间间隔在未来。只需使用 dateByAddingTimeInterval: 像这样: 

NSDate * originalDate = [NSDate date]; 
NSTimeInterval interval = 1; 
NSDate * futureDate = [originalDate dateByAddingTimeInterval:interval]; 

ios获取自1970年以来的毫秒数同java的System.currentTimeMillis() 

Java代码   收藏代码
  1. + (NSString*)generateTimeIntervalWithTimeZone  
  2. {  
  3.     NSMutableString *result = [[NSMutableString alloc] init];  
  4.     NSDate *date = [NSDate date];  
  5.     NSTimeInterval time = [date timeIntervalSince1970]*1000;//毫秒数要乘以1000  
  6.     double i=time;      //NSTimeInterval返回的是double类型  
  7.       
  8.       
  9.     NSDateFormatter *format = [[NSDateFormatter alloc] init];  
  10.     [format setDateFormat:@"Z"];  
  11.     NSString *timeZone = [format stringFromDate:date];  
  12.     NSString *timeIntervalStr = [NSString stringWithFormat:@"%.f", i];  
  13.       
  14.     [result appendString:timeIntervalStr];  
  15.     [result appendString:timeZone];  
  16.       
  17.     return result;  
  18. }  
目录
相关文章
|
API 开发工具 开发者
如何接入公安实名认证接口
对于很多开发者来说,如何接入公安实名认证接口是一项非常重要的任务。在不同的应用场景下,都需要用户进行身份认证,而公安实名认证接口可以有效地帮助开发者完成这一任务。
|
Web App开发 存储 网络安全
Charles抓包以及解决抓取HTTPS请求unknown的问题
Charles抓包以及解决抓取HTTPS请求unknown的问题
10164 1
Charles抓包以及解决抓取HTTPS请求unknown的问题
|
Shell Linux 数据安全/隐私保护
如何在Mac OS下用ISO包制作启动U盘
从U盘启动现在越来越重要了。在没有 cd/dvd 驱动器的情况下,为了安装您选择的操作系统, U盘最简单。事实上, 它甚至可以比刻录 CD 或 DVD 更便宜, 一旦版本过时就扔掉。
4228 0
|
1天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
11天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
449 192
|
3天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。

热门文章

最新文章