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. }  
目录
相关文章
|
移动开发 Dart 前端开发
从架构到源码:一文了解Flutter渲染机制
Flutter从本质上来讲还是一个UI框架,它解决的是一套代码在多端渲染的问题。在渲染管线的设计上更加精简,加上自建渲染引擎,相比ReactNative、Weex以及WebView等方案,具有更好的性能体验。本文将从架构和源码的角度详细分析Flutter渲染机制的设计与实现。较长,同学们可收藏后再看。
7405 1
从架构到源码:一文了解Flutter渲染机制
|
4月前
|
网络协议 Unix
`AF_UNIX` 和 `AF_LOCAL`
`AF_UNIX` 和 `AF_LOCAL`
352 1
|
Swift iOS开发
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
|
10月前
|
JSON 前端开发 JavaScript
在 React 中获取数据的6种方法
在 React 中获取数据的6种方法
290 0
|
缓存 开发工具 git
ShiftMediaProject具体使用
ShiftMediaProject具体使用
321 0
|
存储 Unix Linux
[✔️] android so库的相关命令
[✔️] android so库的相关命令
283 0
|
JavaScript 数据格式
基于Vue实现多标签选择器
基于Vue实现多标签选择器
201 0
基于Vue实现多标签选择器
|
Web App开发 监控 Java
java线上服务问题排查总结
java线上服务问题排查 1、业务日志相关 如果应用系统出现异常,一般都会在业务日志中体现 查看日志问题常用命令,以标装springboot应用为例: 进到标装日志目录:cd /wls/applogs/rtlog/spri* --善用tab键 统计当天业务日志中ERROR出现数量:egre.
47286 0