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

问题描述 投票:140回答:10

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

ios iphone nsdate unix-timestamp
10个回答
267
投票

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

- (NSTimeInterval)timeIntervalSince1970

0
投票

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

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

69
投票

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];

26
投票

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

NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];

9
投票
- (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

针对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)

5
投票

如果要将这些时间存储在数据库中或通过服务器发送...最好是使用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;

4
投票

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

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

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

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


1
投票
 [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

© www.soinside.com 2019 - 2024. All rights reserved.