回答
在這種情況下,你只需要劃分。
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的日期和時間編程指南可以幫助您。
試試這個,
int forHours = seconds/3600,
remainder = seconds % 3600,
forMinutes = remainder/60,
forSeconds = remainder % 60;
,你可以用它按照相同的程序
這是更清潔和更簡潔的方式。 – 2013-08-29 16:56:13
好的方法。如果秒不是一個整數類型,那麼我們不能做像** remaining = seconds%3600 **這樣的操作。 – Gangadhar 2014-09-23 08:01:26
這個問題實際上提到它是一個整數。 – 2014-09-24 17:03:38
使用內置功能的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;
}
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」。
我知道這是一箇舊帖子,但我仍想分享。以下代碼是我使用的。
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中打開一個遊樂場並嘗試一下。
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));
只是有點變形兩者兼容的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;
通過可以轉換到數天,數週,數月,數年以同樣的方式
試着做以下幾點:
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")
您會得到總天數,小時,分鐘和秒秒(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]);
}
這是我的轉換秒來秒,分鐘和小時(只用幾秒鐘的總量和他們每個人之間的關係)算法:
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
有了這個,你可以添加任何你想要的單位,你只需要計算什麼關係是在秒和你想要的單位之間。希望你能理解我對每一步的解釋。如果不是,只要問一下,我可以進一步解釋。
- 1. 將工作秒數轉換爲周,天,小時和分鐘
- 2. 將秒轉換爲小時和分鐘
- 3. 將總分鐘轉換爲天數,小時數,分鐘數vb
- 4. 將秒轉換爲分鐘,小時和天的Java代碼
- 5. 如何將毫秒,秒,分鐘,小時轉換爲SQL Server 2005中的天數?
- 6. 將秒轉換爲天:小時:分鐘:秒
- 7. SQL將秒數轉換爲分鐘數到小時數
- 8. 將分鐘轉換爲小時,分鐘和秒
- 9. 如何將270921sec轉換爲天+小時+分鐘+秒? (紅寶石)
- 10. SQL將毫秒轉換爲天,小時,分鐘
- 11. 將NSTimeInterval轉換爲天,小時,分鐘,秒
- 12. 如何將秒轉換爲小時分鐘和秒?
- 13. 將毫秒轉換爲小時,分鐘和秒python
- 14. 如何將小時,分鐘和秒轉換爲秒
- 15. 如何將秒數轉換成Bash中的分鐘和秒鐘?
- 16. 如何在SQL中將分鐘轉換爲小時分鐘和秒鐘?
- 17. 將毫秒轉換爲天,小時,分和秒
- 18. 將秒轉換爲天,小時,分和秒
- 19. 將秒數轉換爲總時數:分鐘:秒
- 20. Mysql將分鐘轉換爲一天的小時分鐘
- 21. 將秒數拆分爲幾天,幾小時,幾分鐘和幾秒?
- 22. 如何將分鐘轉換爲PHP和負數分鐘的小時和分鐘?
- 23. 將數字秒轉換爲分鐘powerBI
- 24. MySQL的:轉換秒至幾天,分鐘和小時
- 25. 如何將Java時代以來的秒數轉換爲小時/分鐘/秒?
- 26. 將秒轉換爲天,分和秒
- 27. 將ISO8601(我認爲)轉換爲小時:分鐘:Powershell中的秒
- 28. C++轉換小時,分鐘,秒到秒
- 29. php - 轉換小時:分鐘:秒。秒
- 30. 時間戳爲天,小時,分鐘,秒
謝謝。不過,我希望有一些內置的方法。 – higginbotham 2009-02-21 14:13:00
@higginbotham - 如果它對你有幫助,你應該將這個答案標記爲正確。 – Moshe 2011-03-31 20:30:40