首页 文章

如何将NSDate转换为unix时间戳iphone sdk?

提问于
浏览
138

如何将 NSDate 转换为Unix时间戳?我没有发现任何与我的问题相关的内容 .

10 回答

  • 4

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

    - (NSTimeInterval)timeIntervalSince1970
    
  • 1

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

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

    由于它们都具有相同的引用(午夜1Jan1970 UTC)并且都在几秒钟内转换很容易,将NSTimeInterval转换为time_t,根据您的需要进行舍入或截断:

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

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

    int timestamp = [[NSDate date] timeIntervalSince1970];
    
  • 262
    - (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时间 .

  • 6

    斯威夫特

    针对Swift 3进行了更新

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

    Notes

    • timeIntervalSince1970 返回TimeInterval,这是 Double 的类型 .

    • 如果你想走另一条路,你可以做以下事情:

    let myDate = Date(timeIntervalSince1970: myTimeStamp)
    
  • 26

    如果要将这些时间存储在数据库中或通过服务器发送...最好是使用Unix时间戳 . 这是一个小小的代码片段:

    + (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];  
    }
    
  • 5

    我首选的方法是:

    NSDate.date.timeIntervalSince1970;
    
  • 5

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

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

    根据我的经验 - 不要使用timeIntervalSince1970,它给出了纪元时间戳 - 你在GMT后面的秒数 .

    曾经有[[NSDate date] timeIntervalSince1970]的错误,它用于根据手机的时区添加/减去时间,但现在似乎已经解决了 .

  • 0
    [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

  • 9

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

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

相关问题