2009-02-21 103 views

回答

41

在這種情況下,你只需要劃分。

days = num_seconds/(60 * 60 * 24); 
num_seconds -= days * (60 * 60 * 24); 
hours = num_seconds/(60 * 60); 
num_seconds -= hours * (60 * 60); 
minutes = num_seconds/60; 

對於更復雜的日期計算,如天的下午3:00以後千萬秒1月19日之內,1983年的數字,你可以使用NSCalendar類NSDateComponents一起。 Apple的日期和時間編程指南可以幫助您。

+0

謝謝。不過,我希望有一些內置的方法。 – higginbotham 2009-02-21 14:13:00

+0

@higginbotham - 如果它對你有幫助,你應該將這個答案標記爲正確。 – Moshe 2011-03-31 20:30:40

32

試試這個,

int forHours = seconds/3600, 
remainder = seconds % 3600, 
forMinutes = remainder/60, 
forSeconds = remainder % 60; 

,你可以用它按照相同的程序

+2

這是更清潔和更簡潔的方式。 – 2013-08-29 16:56:13

+0

好的方法。如果秒不是一個整數類型,那麼我們不能做像** remaining = seconds%3600 **這樣的操作。 – Gangadhar 2014-09-23 08:01:26

+0

這個問題實際上提到它是一個整數。 – 2014-09-24 17:03:38

3

使用內置功能的strftime以獲得更多的細節,天,周,月和年() :

static char timestr[80]; 
char * HHMMSS (time_t secs) 
{ 
    struct tm ts; 
    ts = *localtime(&secs); 
    strftime(timestr, sizeof(timestr), "%a %b %d %H:%M:%S %Y %Z", &ts); // "Mon May 1, 2012 hh:mm:ss zzz" 
    return timestr; 
} 
6

Objective-C中最受歡迎的方式可能就是這個,正如Apple在他們的文檔中推薦的那樣。

NSDate *startDate = [NSDate date]; 
    NSDate *endDate = [NSDate dateWithTimeInterval:(365*24*60*60*3+24*60*60*129) sinceDate:startDate]; 

    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; 
    NSDateComponents *components = [gregorian components:unitFlags 
               fromDate:startDate 
                toDate:endDate options:0]; 
    NSInteger years = [components year]; 
    NSInteger months = [components month]; 
    NSInteger days = [components day]; 
    NSLog(@"years = %ld months = %ld days = %ld",years,months,days); 

確保不檢索未使用unitFlags定義的組件,整數將爲「undefined」。

7

我知道這是一箇舊帖子,但我仍想分享。以下代碼是我使用的。

int seconds = (totalSeconds % 60); 
int minutes = (totalSeconds % 3600)/60; 
int hours = (totalSeconds % 86400)/3600; 
int days = (totalSeconds % (86400 * 30))/86400; 

第一行 - 我們在一分鐘內除以秒數後得到秒的餘數。

第二行 - 我們在除以一小時後的秒數後得到秒的剩餘秒數。然後我們在一分鐘內將其分成幾秒。

第三行 - 除以一天中的秒數後,我們得到秒的剩餘秒數。然後我們用一小時內的秒數來除。

第四行 - 除以一個月內的秒數後,我們得到秒的餘數。然後我們將其除以一天中的秒數。 我們可以只使用天以下...

int days = totalSeconds/86400; 

但是,如果我們使用上面的線,並想繼續,並得到我們最終會1個月和30天的月份,當我們希望得到公正1個月。

在Xcode中打開一個遊樂場並嘗試一下。

0
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
    Date dateObj1=null; 
    Date dateObj2=null; 
    try { 
     // String format = "MM/dd/yyyy hh:mm:ss"; 
     dateObj1 = sdf.parse(Appconstant.One_One_time); 
     dateObj2 = sdf.parse(Appconstant.One_Two_time); 
     Log.e(TAG, "dateObj12" + dateObj1.toString() + "---" + dateObj2.toString()); 
     DecimalFormat crunchifyFormatter = new DecimalFormat("###,###"); 
     long diff = dateObj2.getTime() - dateObj1.getTime(); 
     /*int diffDays = (int) (diff/(24 * 60 * 60 * 1000)); 
     System.out.println("difference between days: " + diffDays); 
     int diffhours = (int) (diff/(60 * 60 * 1000)); 
     System.out.println("difference between hours: " 
       + crunchifyFormatter.format(diffhours)); 
     int diffmin = (int) (diff/(60 * 1000)); 
     System.out.println("difference between minutes: " 
       + crunchifyFormatter.format(diffmin));*/ 
     int diffsec = (int) (diff/(1000)); 
     System.out.println("difference between seconds:" 
       + crunchifyFormatter.format(diffsec)); 
2

只是有點變形兩者兼容的32位& 64位。使用NSInteger系統將根據32/64位自動轉換爲int或long。

NSInteger seconds = 10000; 
NSInteger remainder = seconds % 3600; 
NSInteger hours = seconds/3600; 
NSInteger minutes = remainder/60; 
NSInteger forSeconds = remainder % 60; 

通過可以轉換到數天,數週,數月,數年以同樣的方式

-1

試着做以下幾點:

x=int(input("enter a positive integer: ")) 
    sec = int(x % 60); 
    minu =int((x % 3600)/60); 
    ho =int((x % 86400)/3600); 
    days =int(x/(60 * 60 * 24)); 
    print(int(days),"days", ho,"hours", minu , "minutes" , sec ,"seconds") 
0

您會得到總天數,小時,分鐘和秒秒(self.secondsLeft)

days = self.secondsLeft/86400; 
hours = (self.secondsLeft %86400)/3600; 
minutes = (self.secondsLeft % 3600)/60; 
seconds = (self.secondsLeft %3600) % 60; 

代碼 ......

[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES]; 

-(void) updateCountdown { 

    int days,hours,minutes,seconds; 
    self.secondsLeft--; 

    days = self.secondsLeft/86400; 
    hours = (self.secondsLeft %86400)/3600; 
    minutes = (self.secondsLeft % 3600)/60; 
    seconds = (self.secondsLeft %3600) % 60; 

    NSLog(@"Time Remaining =%@",[NSString stringWithFormat:@"%02d:%02d:%02d:%02d",days, hours, minutes,seconds]); 
} 
1

這是我的轉換秒來秒,分鐘和小時(只用幾秒鐘的總量和他們每個人之間的關係)算法:

int S = totalSeconds % BaseSMH 

int M = ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH^2)/BaseSMH 

int H = (totalSeconds - totalSeconds % BaseSMH - ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH^2))/BaseSMH^2 

這裏如下我對它的解釋:

試驗時間轉換爲秒: HH:MM:SS = 2時20分10秒=> totalSeconds = 2 * 3600 + 20 * 60 + 10 = 7200 + 1200 + 10 = 8410

基部之間秒 - >分鐘和分鐘 - >小時是60(從秒 - >小時,它是60^2 = 3600)。

int totalSeconds = 8410 
const int BaseSMH = 60 

每個單元(在此由一個變量表示)可以通過從秒的總量刪除以前的單元(以秒錶示),並通過它的秒,我們試圖在單元之間的關係將其劃分來計算計算。這是每個calulation看起來像使用該算法:

int S = totalSeconds % BaseSMH 

int M = ((totalSeconds - S) % BaseSMH^2)/BaseSMH 

int H = (totalSeconds - S - M * BaseSMH)/BaseSMH^2 

與所有的數學,我們現在可以在與分鐘和小時計算,每個單元代替我們如何計算以前的單位,然後將每個計算將像這樣(記住,與BaseSMH除以在M的計算和在H calulation通過BaseSMH乘以相互抵消):

int S = totalSeconds % BaseSMH 

int M = ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH^2)/BaseSMH 

int H = (totalSeconds - totalSeconds % BaseSMH - ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH^2))/BaseSMH^2 

測試用「totalSeconds」和「BaseSMH」從上面看起來像這樣:

int S = 8410 % 60 

int M = ((8410 - 8410 % 60) % 60^2)/60 

int H = (8410 - 8410 % 60 - ((8410 - 8410 % 60) % 60^2))/60^2 

計算S:

int S = 8410 % 60 = 10 

計算L:

int M = ((8410 - 8410 % 60) % 60^2)/60 
= ((8410 - 10) % 3600)/60 
= (8400 % 3600)/60 
= 1200/60 
= 20 

計算H:

int H = (8410 - 8410 % 60 - ((8410 - 8410 % 60) % 60^2))/60^2 
= (8410 - 10 - ((8410 - 10) % 3600))/3600 
= (8400 - (8400 % 3600))/3600 
= (8400 - 1200)/3600 
= 7200/3600 
= 2 

有了這個,你可以添加任何你想要的單位,你只需要計算什麼關係是在秒和你想要的單位之間。希望你能理解我對每一步的解釋。如果不是,只要問一下,我可以進一步解釋。

相關問題