欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS NSTimeInterval使用案例詳解

 更新時(shí)間:2021年08月28日 15:18:49   作者:conanwin  
這篇文章主要介紹了IOS NSTimeInterval使用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一 ios 獲取時(shí)間間隔

想在程序開始或者進(jìn)入某個(gè)界面 ,到結(jié)束程序或退出某個(gè)界面,獲取到這個(gè)持續(xù)時(shí)間. 獲取到這個(gè)時(shí)間還需要轉(zhuǎn)化一個(gè)取得時(shí)分秒.
-(NSString *)getCurrentTime

{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *dateTime = [formatter stringFromDate:[NSDate date]];

    self.startTime = dateTime;

    return startTime;

}

date1代表開始時(shí)間,在開始計(jì)時(shí)的地方調(diào)用 [self getCurrentTime]; 在結(jié)束時(shí)的方法里寫如下代碼:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *date1 = [formatter dateFromString:startTime];

NSDate *date2 = [NSDate date];

NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];

 

int hour = (int)(aTimer/3600);

int minute = (int)(aTimer - hour*3600)/60;

int second = aTimer - hour*3600 - minute*60;

NSString *dural = [NSString stringWithFormat:@"%d時(shí)%d分%d秒", hour, minute,second];

二 我想把它轉(zhuǎn)換成分鐘和秒。

比如我有:“326.4”秒,我想把它轉(zhuǎn)換成下面的字符串: “5:26”。 什么是實(shí)現(xiàn)這一目標(biāo)的最佳方法是什么?

1. 偽代碼:

minutes = floor(326.4/60)
seconds = round(326.4 - minutes * 60)

2. 簡(jiǎn)述 從布賴恩・拉姆齊答案是更方便的,如果你只是想轉(zhuǎn)換為分鐘。 如果你想Cocoa API的為你做它和轉(zhuǎn)換您不僅分鐘,但也給天,月,星期等,...我認(rèn)為這是一個(gè)更通用的方法 使用

(NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts“返回,作為NSDateComponents兩者之間的差異提供的日期”。從API 創(chuàng)建2 NSDate的 CodeGo.net,其區(qū)別是你要轉(zhuǎn)換。 (如果你的2 NSDate的你不需要做這一步,你甚至不需要的 讓你的quotes從NSDateComponents 示例代碼

// The time interval 
NSTimeInterval theTimeInterval = 326.4;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);
[date1 release];
[date2 release];

已知問(wèn)題 太多的只是一個(gè)轉(zhuǎn)換,你是對(duì)的,但是這是API如何工作的。 我的建議:如果你要管理你的NSDate和NSCalendar,該API將做艱苦的工作適合你。 

3. ,作為一個(gè)堆棧處女...我不知道如何回答布賴恩・拉姆齊的回答... 使用圓不會(huì)為59.5和59.99999之間第二值工作。第二個(gè)值將是60在此期間。使用TRUNC而不是...

double progress;
int minutes = floor(progress/60);
int seconds = trunc(progress - minutes * 60);

4. 布賴恩・拉姆齊的代碼,去pseudofied:

- (NSString*)formattedStringForDuration:(NSTimeInterval)duration
{
 NSInteger minutes = floor(duration/60);
 NSInteger seconds = round(duration - minutes * 60);
 return [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
}

5. 所有這些看起來(lái)比他們需要!這里有一個(gè)簡(jiǎn)短而親切的方式來(lái)轉(zhuǎn)換的時(shí)間間隔為小時(shí),分鐘和秒:

NSTimeInterval timeInterval = 326.4;
long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
int hour = seconds / 3600
int mins = (seconds % 3600) / 60;
int secs = seconds % 60;

請(qǐng)注意,當(dāng)你把一個(gè)浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù),你得到樓()自動(dòng)完成,但你可以,如果要是讓你感覺更好:-)它添加到的前兩個(gè) 

6. 因?yàn)樗举|(zhì)上是一個(gè)雙... 60.0劃分和提取的組成部分和小數(shù)部分。 的組成部分,將是分鐘的整數(shù)。 再乘以小數(shù)部分按60.0。 其結(jié)果將是剩下秒。

到此這篇關(guān)于IOS NSTimeInterval使用案例詳解的文章就介紹到這了,更多相關(guān)IOS NSTimeInterval使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論