如何将 nstimeinterval是什么 转换为 int

IOS&标准时间与时间戳&相互转化
//时间转成时间戳
&&NSTimeZone&*zone
= [NSTimeZone&defaultTimeZone];//获得当前应用程序默认的时区
&&NSInteger&interval
[zone&secondsFromGMTForDate:[NSDate&date]];//以秒为单位返回当前应用程序与世界标准时间(格林威尼时间)的时差
&&NSDate&*localeDate
[[NSDate&date]&dateByAddingTimeInterval:interval];
&&NSTimeInterval&timeInterval2
= [localeDate&timeIntervalSince1970];
&&NSLog(@"%f",timeInterval2);
&&//时间戳转成时间
&&NSTimeInterval&timeInterval
= [@".211"&doubleValue];
&&NSDate&*date2
[NSDate&dateWithTimeIntervalSince1970:timeInterval];
&&NSLog(@" date2
-------------------- %@",date2);
//////////////////////////////////////////////////////////////////////////////////////////////
(NSString&*)intervalSinceNow:
(NSString&*) theDate
&&NSDateFormatter&*date=[[NSDateFormatter&alloc]&init];
&&[date&setDateFormat:@"yyyy-MM-dd
HH:mm:ss"];
&&NSDate&*d=[date&dateFromString:theDate];
&&NSTimeInterval&late=[d&timeIntervalSince1970]*1;
&&NSDate* dat =
[NSDate&dateWithTimeIntervalSinceNow:0];
&&NSTimeInterval&now=[dat&timeIntervalSince1970]*1;
&&NSString&*timeString=@"";
&&NSTimeInterval&cha=now-
&&if&(cha/3600&&span
style="color: rgb(140, 139, 252); "&1) {
&&timeString =
[NSString&stringWithFormat:@"%f",
&&timeString =
[timeString&substringToIndex:timeString.length-7];
&&timeString=[NSString&stringWithFormat:@"%@分钟前",
timeString];
&&if&(cha/3600&1&&cha/86400&&span
style="color: rgb(140, 139, 252); "&1) {
&&timeString =
[NSString&stringWithFormat:@"%f",
cha/3600];
&&timeString =
[timeString&substringToIndex:timeString.length-7];
&&timeString=[NSString&stringWithFormat:@"%@小时前",
timeString];
&&if&(cha/86400&1)
&&timeString =
[NSString&stringWithFormat:@"%f",
cha/86400];
&&timeString =
[timeString&substringToIndex:timeString.length-7];
&&timeString=[NSString&stringWithFormat:@"%@天前",
timeString];
&&[date&release];
&&return&timeS
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。iphone获取1970年以来的毫秒数 - pengyingh - 博客园
Object-C获取自1970年以来的毫秒数
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval返回的是double类型,输出会显示为10位整数加小数点加一些其他值
// 如果想转成int型,必须转成long long型才够大。
&&&&NSTimeInterval&time = [[NSDate date]&timeIntervalSince1970];
&&&&long&long&dTime = [[NSNumber numberWithDouble:time]&longLongValue];&// 将double转为long long型
&&&&NSString&*curTime = [NSString stringWithFormat:@"%llu",dTime];&// 输出long long型iphone - How to convert NSTimeInterval to int? - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
How do I convert NSTimeInterval into an Integer value?
My TimeInterval holds the value 83.01837. I need to convert it into 83. I have googled but couldn't find any help.
59.3k1183130
1,82173565
Direct assignment:
NSTimeInterval interval = 2542;
NSInteger time =
//time is now equal to 1002343
NSTimeInterval is a double, so if you assign it directly to a NSInteger (or int, if you wish) it'll work.
This will cut off the time to the nearest second.
If you wish to round to the nearest second (rather than have it cut off) you can use round before you make the assignment:
NSTimeInterval interval = 2542;
NSInteger time = round(interval);
//time is now equal to 1002344
6,76922355
According to , NSTimeInterval is just a double:
typedef double NSTimeI
You can cast this to an int:
seconds = (int) myTimeI
Watch out for overflows, though!
59.3k1183130
I suspect that NSTimeInterval values from NSDate would overflow an NSInteger. You'd likely want a long long. (64 bit integer.) Those can store honking-big integer values (-2^63 to 2^63 -1)
long long integerSeconds = round([NSDate timeIntervalSinceReferenceDate]);
56.8k959123
In Swift 3.0
let timestamp = round(NSDate().timeIntervalSince1970)
I had a need to store an NSDate in a Swift Number.
I used the following cast which is working great.
Double(startDateTime.timeIntervalSince1970)
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enablediOS 怎样将标准时间转换为几小时前,几天前?_百度知道

我要回帖

更多关于 nstimeinterval转换 的文章

 

随机推荐