【问题标题】:How to use the current time in Objective-C如何在 Objective-C 中使用当前时间
【发布时间】:2014-10-17 03:50:58
【问题描述】:

我应该如何将当前时间导入我的应用程序并使用它来进行一些计算?

我的应用程序所做的是它使用当前时间并进行一些计算,然后给我一些百分比。

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    您希望如何获取时间(以毫秒为单位?)

    这里已经有几个问题了 - 比如this 一个,它应该可以帮助您指出正确的方向。

    来自NSDate 的 Apple 文档

    创建并返回一个设置为给定数量的 NSDate 对象 距离格林威治标准时间 1970 年 1 月 1 日第一刻的秒数。

    [[NSDate date] timeIntervalSince1970];

    【讨论】:

      【解决方案2】:
      // use this method to get current time as per your iPhone's time format
          +(NSString *)getCurrTime
          {
              NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
              [formatter setLocale:[NSLocale currentLocale]];
              [formatter setDateStyle:NSDateFormatterNoStyle];
              [formatter setTimeStyle:NSDateFormatterShortStyle];
              NSString *dateString = [formatter stringFromDate:[NSDate date]];
              NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
              NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
      
              BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
              if (is24h == YES) {
                  [formatter setDateFormat:@"HH:mm"];
              }
              else
              {
                  [formatter setDateFormat:@"hh:mm a"];
              }
              dateString =[formatter stringFromDate:[NSDate date]];
              NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));
              return dateString;
          }
      

      【讨论】:

        猜你喜欢
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多