如何将一个转换NSDate
为 Unix 时间戳?我读过很多相反的帖子。但我没有找到与我的问题相关的任何内容。
10 回答
我相信这是您正在寻找的 NSDate 选择器:
- (NSTimeInterval)timeIntervalSince1970
Unix 时间戳是自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 时间戳日期:
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
- (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 时间。
迅速
为 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)
如果您想将这些时间存储在数据库中或通过服务器发送...最好使用 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];
}
我的首选方式很简单:
NSDate.date.timeIntervalSince1970;
根据@kexik 的建议,使用 UNIX 时间函数如下:
time_t result = time(NULL);
NSLog([NSString stringWithFormat:@"The current Unix epoch time is %d",(int)result]);
.根据我的经验-不要使用 timeIntervalSince1970 ,它会给出纪元时间戳-您落后于格林尼治标准时间的秒数。
[[NSDate date]timeIntervalSince1970] 曾经有一个错误,它曾经根据手机的时区添加/减去时间,但现在似乎已解决。
[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
如果您需要时间戳作为字符串。
time_t result = time(NULL);
NSString *timeStampString = [@(result) stringValue];