如何将当前日期的iOS日期格式转换为整数?

时间:2014-07-18 19:13:12

标签: ios objective-c nsdate

我正在试图弄清楚如何将当前日期和时间转换为秒数?

我看过这篇文章What format string do I use for milliseconds in date strings on iPhone?但我实际上是想把它变成一个数字......

编辑:抱歉意味着秒

3 个答案:

答案 0 :(得分:3)

如果你想要UTC时间戳并且希望存储在int而不是32位,则不可能使用long long来代替NSString *dateValue = @"2011-06-23T13:13:00.000"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSString *formatString = @"yyyy-MM-dd'T'HH:mm:ss.SSS"; [formatter setDateFormat:formatString]; NSDate *date = [formatter dateFromString:dateValue]; long long dateInMillis = [date timeIntervalSince1970]*1000; //This will give milliseconds long dateInSeconds = [date timeIntervalSince1970]; //This will give seconds ,它具有更大的范围并且可以用作整数

long

编辑:对于//This will give seconds of current date long dateInSeconds = [[NSDate date] timeIntervalSince1970]; 值使用

中的当前日期
{{1}}

答案 1 :(得分:1)

您可以使用以下内容将NSDate转换为double

NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];

答案 2 :(得分:1)

米利斯:

long long milliseconds = llround(([[NSDate date] timeIntervalSince1970] * 1000.0));