【问题标题】:How to convert NSDate into unix timestamp iphone sdk?如何将 NSDate 转换为 unix 时间戳 iphone sdk?
【发布时间】:2011-03-01 03:17:04
【问题描述】:

如何将NSDate 转换为 Unix 时间戳?我读过很多相反的帖子。但我没有找到与我的问题相关的任何内容。

【问题讨论】:

    标签: ios iphone nsdate unix-timestamp


    【解决方案1】:

    我相信这是您正在寻找的 NSDate 选择器:

    - (NSTimeInterval)timeIntervalSince1970
    

    【讨论】:

    • 如果提供了 nsdate,这会返回 unix 时间戳吗?
    • 是的,这正是您要找的。 UNIX 以 1970 年 1 月 1 日以来的秒数为单位测量时间,并且此方法返回接收者(您提供的 NSDate)与 1970 年 1 月 1 日格林威治标准时间的第一时刻之间的时间间隔。 NSTimeInterval 实际上是 double 的 typedef。
    • 获取字符串 (28/11/2011 14:14:13 1322486053) : [NSString stringWithFormat:@"%.0f", [aDate timeIntervalSince1970]];
    • 该方法一般会根据设备上设置的时间而改变。我做了一个测试,我在中午 12 点得到时间戳,然后在 iPad 上将时间更改为晚上 8 点左右,这两个数字的差异非常大。如果时间戳与设备时间无关,则数字应该仅相差几秒钟,但在这种情况下,我不得不得出结论,iPad 上的时间确实会影响 unixTimeStamp
    • @Esko918 当然可以。 Unix 时间描述了从 1970 年到您要指定的时间点的秒数(在本例中为“现在”)。如果您更改 iPad 上的时间,您实际上会将“现在”更改为其他时间,从而导致时间戳的差异。
    【解决方案2】:

    Unix timestamp 是自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数。它由类型 time_t 表示,通常是带符号的 32 位整数类型(long 或 int)。

    iOS 为 NSDate 对象提供 -(NSTimeInterval)timeIntervalSince1970,它返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来的秒数。NSTimeInterval 是双浮点类型,因此您可以获得秒数和秒的小数部分。

    由于它们都具有相同的参考(UTC 时间 1 月 1 日午夜)并且都以秒为单位进行转换很容易,将 NSTimeInterval 转换为 time_t,根据您的需要进行舍入或截断:

    time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
    

    【讨论】:

    • 此功能显示正确的日期,但时间错误。它与系统时间不匹配。
    • Unix 时间是 UTC (GMT),印度是 UTC+5.5。 5.5 小时后关闭吗?
    • 您的系统时间显示的是您当地时区的时间,这很好。 Unix 时间戳不是本地时间,它始终是 UTC 时间,印度的时间差为 +5:30,这里的时间差为 -7:00。没错。
    • 那么如何使用呢?我可以在以后的计算中使用它吗?我有一个来自在线 PHP 数据库的 UNIX 时间戳,然后是这个。我想比较这两者,以便决定是否下载有关对象的最新数据。
    • 我现在正在这样做:int unixTime = floor([piece.piece_last_view timeIntervalSince1970]) 其中piece.piece.last_view 是一个NSDate
    【解决方案3】:

    您可以通过这种方式从日期创建 unix 时间戳日期:

    NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
    

    【讨论】:

    • 这是错误的,timeIntervalSince1970 返回的 TimeInterval 实际上是一个 double。
    【解决方案4】:
    - (void)GetCurrentTimeStamp
        {
            NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
            [objDateformat setDateFormat:@"yyyy-MM-dd"];
            NSString    *strTime = [objDateformat stringFromDate:[NSDate date]];
            NSString    *strUTCTime = [self GetUTCDateTimeFromLocalTime:strTime];//You can pass your date but be carefull about your date format of NSDateFormatter.
            NSDate *objUTCDate  = [objDateformat dateFromString:strUTCTime];
            long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
    
            NSString *strTimeStamp = [Nsstring stringwithformat:@"%lld",milliseconds];
            NSLog(@"The Timestamp is = %@",strTimestamp);
        }
    
     - (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            NSDate  *objDate    = [dateFormatter dateFromString:IN_strLocalTime];
            [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
            NSString *strDateTime   = [dateFormatter stringFromDate:objDate];
            return strDateTime;
        }
    

    注意:- 时间戳必须在 UTC 区域,所以我将我们的本地时间转换为 UTC 时间。

    【讨论】:

      【解决方案5】:

      斯威夫特

      为 Swift 3 更新

      // current date and time
      let someDate = Date()
      
      // time interval since 1970
      let myTimeStamp = someDate.timeIntervalSince1970
      

      备注

      • timeIntervalSince1970 返回TimeInterval,这是Double 的类型别名。

      • 如果你想走另一条路,你可以这样做:

          let myDate = Date(timeIntervalSince1970: myTimeStamp)
        

      【讨论】:

        【解决方案6】:

        如果您想将这些时间存储在数据库中或通过服务器发送...最好使用 Unix 时间戳。这是一个小sn-p来获得它:

        + (NSTimeInterval)getUTCFormateDate{
        
            NSDateComponents *comps = [[NSCalendar currentCalendar] 
                                       components:NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit 
                                       fromDate:[NSDate date]];
            [comps setHour:0];
            [comps setMinute:0];    
            [comps setSecond:[[NSTimeZone systemTimeZone] secondsFromGMT]];
        
            return [[[NSCalendar currentCalendar] dateFromComponents:comps] timeIntervalSince1970];  
        }
        

        【讨论】:

        • 为什么在组件中将小时和分钟设置为 0?此外,它如何根据以下内容为您提供准确的 UTC:[[NSTimeZone systemTimeZone] secondsFromGMT]]?请解释一下。
        • 你好朋友它帮助了我
        【解决方案7】:

        我喜欢的方式很简单:

        NSDate.date.timeIntervalSince1970;
        

        【讨论】:

          【解决方案8】:

          根据@kexik 的建议,使用 UNIX 时间函数如下:

            time_t result = time(NULL);
            NSLog([NSString stringWithFormat:@"The current Unix epoch time is %d",(int)result]);
          

          。根据我的经验 - 不要使用 timeIntervalSince1970 ,它会给出纪元时间戳 - 您落后 GMT 的秒数。

          以前 [[NSDate date]timeIntervalSince1970] 有一个错误,它曾经根据手机的时区添加/减去时间,但现在似乎已解决。

          【讨论】:

          • 我正在纠正我的答案 - 它似乎真的是平等的。虽然 time(NULL) 可能会快一点,因为它没有调用 obj-c 运行时。 :P 测试代码 NSLog(@"time(NULL) = %d ; timeIntervalSince1970 = %d", (int)time(NULL), (int)[[NSDate date] timeIntervalSince1970]);
          • 我遇到了 [[NSDate date]timeIntervalSince1970] 的问题。他们似乎也考虑了您所在的地区。
          • 是的,这就是我认为我那天所经历的,但在 iOS7 模拟器和具有 GMT+2 时区的 iOS6 设备上,它们现在看起来相等......无论如何,时间(NULL)总是有效的 :)跨度>
          【解决方案9】:
           [NSString stringWithFormat: @"first unixtime is %ld",message,(long)[[NSDate date] timeIntervalSince1970]];
          
           [NSString stringWithFormat: @"second unixtime is %ld",message,[[NSDate date] timeIntervalSince1970]];
          
           [NSString stringWithFormat: @"third unixtime is %.0f",message,[[NSDate date] timeIntervalSince1970]]; 
          

          第一个unixtime 1532278070

          第二个unixtime 1532278070.461380

          第三个unixtime 1532278070

          【讨论】:

            【解决方案10】:

            如果您需要时间戳作为字符串。

            time_t result = time(NULL);                
            NSString *timeStampString = [@(result) stringValue];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-03-02
              • 1970-01-01
              • 2015-01-06
              • 2012-03-19
              • 2012-06-10
              • 2012-06-10
              相关资源
              最近更新 更多