ios 如何将 NSDate 转换为 unix 时间戳 iphone sdk?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2997062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert NSDate into unix timestamp iphone sdk?
提问by neha
How to convert an NSDate
into Unix timestamp? I've read many posts which do the reverse. But I'm not finding anything related to my question.
如何将 an 转换NSDate
为 Unix 时间戳?我读过很多相反的帖子。但我没有找到与我的问题相关的任何内容。
回答by BAndonovski
I believe this is the NSDate's selector you're looking for:
我相信这是您正在寻找的 NSDate 选择器:
- (NSTimeInterval)timeIntervalSince1970
回答by progrmr
A Unix timestampis the number of seconds since 00:00:00 UTC January 1, 1970. It's represented by the type time_t, which is usually a signed 32-bit integer type (long or int).
甲Unix时间戳是从1970年开始它的由类型表示00:00:00 UTC 1月1日的秒数time_t的,它通常是一个带符号的32位整数类型(长或INT)。
iOS provides -(NSTimeInterval)timeIntervalSince1970for NSDate objects which returns the number of seconds since 00:00:00 GMT January 1, 1970. NSTimeInterval is a double floating point type so you get the seconds and fractions of a second.
iOS为 NSDate 对象提供-(NSTimeInterval)timeIntervalSince1970,它返回自 1970 年 1 月 1 日格林威治标准时间 00:00:00 以来的秒数。 NSTimeInterval 是双浮点类型,因此您可以获得秒和秒的分数。
Since they both have the same reference (midnight 1Jan1970 UTC) and are both in seconds the conversion is easy, convert the NSTimeInterval to a time_t, rounding or truncating depending on your needs:
由于它们都具有相同的参考(UTC 时间 1 月 1 日午夜)并且都以秒为单位,因此转换很容易,将 NSTimeInterval 转换为 time_t,根据您的需要舍入或截断:
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
回答by Peter Van de Put
You can create a unix timestamp date from a date this way:
您可以通过以下方式从某个日期创建一个 unix 时间戳日期:
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
回答by Vicky
- (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;
}
NOTE :- The Timestamp must be in UTC Zone, So I convert our local Time to UTC Time.
注意:- 时间戳必须在 UTC 区域中,所以我将我们的本地时间转换为 UTC 时间。
回答by Suragch
Swift
迅速
Updated for Swift 3
为 Swift 3 更新
// current date and time
let someDate = Date()
// time interval since 1970
let myTimeStamp = someDate.timeIntervalSince1970
Notes
笔记
timeIntervalSince1970
returnsTimeInterval
, which is a typealias forDouble
.If you wanted to go the other way you could do the following:
let myDate = Date(timeIntervalSince1970: myTimeStamp)
timeIntervalSince1970
返回TimeInterval
,这是Double
.如果您想走另一条路,您可以执行以下操作:
let myDate = Date(timeIntervalSince1970: myTimeStamp)
回答by Alex
My preferred way is simply:
我的首选方法很简单:
NSDate.date.timeIntervalSince1970;
回答by aahsanali
If you want to store these time in a database or send it over the server...best is to use Unix timestamps. Here's a little snippet to get that:
如果您想将这些时间存储在数据库中或通过服务器发送……最好使用 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];
}
回答by Tapan Thaker
As per @kexik's suggestion using the UNIX time function as below :
根据@kexik 的建议,使用 UNIX 时间函数如下:
time_t result = time(NULL);
NSLog([NSString stringWithFormat:@"The current Unix epoch time is %d",(int)result]);
.As per my experience - don't use timeIntervalSince1970 , it gives epoch timestamp - number of seconds you are behind GMT.
.根据我的经验 - 不要使用 timeIntervalSince1970 ,它会给出纪元时间戳 - 你落后格林威治标准时间的秒数。
There used to be a bug with [[NSDate date]timeIntervalSince1970] , it used to add/subtract time based on the timezone of the phone but it seems to be resolved now.
[[NSDate date]timeIntervalSince1970] 曾经有一个错误,它曾经根据手机的时区添加/减去时间,但现在似乎已解决。
回答by user1105951
[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]];
first unixtime 1532278070
第一个unixtime 1532278070
second unixtime 1532278070.461380
第二个unixtime 1532278070.461380
third unixtime 1532278070
第三个unixtime 1532278070
回答by nanjunda
If you need time stamp as a string.
如果您需要时间戳作为字符串。
time_t result = time(NULL);
NSString *timeStampString = [@(result) stringValue];