Objective c 在Obj-c中将秒转换为天、分钟和小时
在objective-c中,如何将整数(表示秒)转换为天、分钟、小时Objective c 在Obj-c中将秒转换为天、分钟和小时,objective-c,time,Objective C,Time,在objective-c中,如何将整数(表示秒)转换为天、分钟、小时 谢谢 在这种情况下,只需进行除法 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; 对于更复杂的日期计算,例如1983年1月19日下午3点
谢谢 在这种情况下,只需进行除法
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;
对于更复杂的日期计算,例如1983年1月19日下午3点后1000万秒内的天数,可以使用NSCalendar类和NSDateComponents。苹果公司的日期和时间编程指南在这里帮助您。试试这个
int forHours = seconds / 3600,
remainder = seconds % 3600,
forMinutes = remainder / 60,
forSeconds = remainder % 60;
通过执行相同的步骤,您可以使用它获取更多的详细信息,如天、周、月和年使用内置函数strftime():
objective-c中最受欢迎的方式可能是这一种,正如苹果在其文档中推荐的那样
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定义的组件,integer将是“未定义的”。我知道这是一篇旧文章,但我还是想与大家分享。下面的代码是我使用的
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中打开一个游乐场并尝试一下。只需对兼容32位和64位的两个版本进行一点修改。使用
NSInteger
系统将根据32/64位自动转换为int或long
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));
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)中获得天、小时、分和秒数 代码
这是我将秒转换为秒、分钟和小时的算法(仅使用秒的总量以及它们之间的关系): 下面是我对它的解释: 测试时间转换为秒: HH:MM:SS=02:20:10=>totalSeconds=2*3600+20*60+10=7200+1200+10=8410 秒->分钟和分钟->小时之间的基数是60(从秒->小时到60^2=3600) 每个单位(这里用一个变量表示)可以通过从总秒数中删除前一个单位(以秒表示)并将其除以秒与我们试图计算的单位之间的关系来计算。这是使用此算法时每个计算的结果:
int S = totalSeconds % BaseSMH
int M = ((totalSeconds - S) % BaseSMH ^ 2) / BaseSMH
int H = (totalSeconds - S - M * BaseSMH) / BaseSMH ^ 2
与所有数学一样,我们现在可以用我们计算前一个单位的方式替换分钟和小时计算中的每个单位,然后每个计算将如下所示(请记住,在M计算中除以BaseSMH,在H计算中乘以BaseSMH会相互抵消):
使用上面的“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
计算M:
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
有了这个,你可以添加任何你想要的单位,你只需要计算秒和你想要的单位之间的关系。希望你能理解我对每一步的解释。如果没有,请提问,我可以进一步解释。起初,我想出了一个类似于fish答案的解决方案:
const secondsToString=s=>{
const secondsOfYear=60*60*24*365;
const secondsOfDay=60*60*24;
常数秒小时=60*60;
const secondsOfMinute=60;
设y=~(s/secondsOfYear);s%=secondsOfYear;
设d=~(s/secondsOfDay);s%=secondsOfDay;
设h=~(s/secondsOfHour);s%=secondsOfHour;
设m=~(s/secondsOfMinute);s%=secondsOfMinute;
y=(y>0?(y+‘y’:“”);
d=(d>0?(d+'d'):'';
h=(h>0?(h+‘h’:“”);
m=(m>0?(m+‘m’:“”);
s=(s>0?(s+'s'):'';
返回y+d+h+m+s;
}
谢谢。我希望会有一些内置的方法,然而,这是一种更干净、更简洁的方法。很好的方法。如果秒不是整数类型,那么我们就不能执行余数=秒%3600这样的操作。问题实际上提到了它是整数。如果执行计算时DTS或任何介于开始日期
和结束日期
之间的值,这难道不会中断吗这是最好的答案
int S = totalSeconds % BaseSMH
int M = ((totalSeconds - S) % BaseSMH ^ 2) / BaseSMH
int H = (totalSeconds - S - M * BaseSMH) / BaseSMH ^ 2
int S = totalSeconds % BaseSMH
int M = ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH ^ 2) / BaseSMH
int H = (totalSeconds - totalSeconds % BaseSMH - ((totalSeconds - totalSeconds % BaseSMH) % BaseSMH ^ 2)) / BaseSMH ^ 2
int S = 8410 % 60
int M = ((8410 - 8410 % 60) % 60 ^ 2) / 60
int H = (8410 - 8410 % 60 - ((8410 - 8410 % 60) % 60 ^ 2)) / 60 ^ 2
int S = 8410 % 60 = 10
int M = ((8410 - 8410 % 60) % 60 ^ 2) / 60
= ((8410 - 10) % 3600) / 60
= (8400 % 3600) / 60
= 1200 / 60
= 20
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