- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试制作一个计数器,显示离我们去欧洲旅行还有多少天。它只有大约 70 天(截至今天)所以我认为我不必担心天文数字或任何东西,但我真的很难过 - 我附上了一些 friend 给我的代码,它不也工作。相信我,我已经尝试了我能想到的一切——在任何人咬我的头之前,我在这些论坛上看到过,是的,我确实非常广泛地查看了 Apple 文档,但我不是 100%确定从哪里开始 - 我已经尝试了 NSTimer、NSDate 和它们所有的子类和方法,但没有任何东西可以立即跳出。
就我认为我实际应该做的事情而言,我认为我需要以某种方式为今天/现在/当天的“天”分配一个整数值,它将使用 [NSDate date 动态变化]
然后对于我们离开的日期也是如此。倒计时只是在再次调用该方法时更新(如果需要,我可以使用 NSTimer 执行此操作)并且倒计时上显示的值是这两个值之间的差值。
我并不是特别想要一种在我们离开之前每秒更新一次的闪烁的东西 - 我个人认为这很俗气,但如果有人知道怎么做,我会很感激它以供将来引用。
我也对 google 进行了广泛的搜索,我可能只是使用了错误的搜索词,但我也找不到任何东西。
非常感谢任何帮助。
非常感谢。
Michaeljvdw
- (void)countDownMethod {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:startDay];
[comps setMonth:startMonth];
[comps setYear:startYear];
[comps setHour:startHour];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSLog(@"%@",date);
[gregorian release];
[comps release];
NSTimeInterval diff = [date timeIntervalSinceNow];
int diffInt = diff;
NSString *days = [NSString stringWithFormat:@"%d",diffInt/86400];
day0.text = @"0";
day1.text = @"0";
day2.text = @"0";
NSLog(@"Days Length: %d",days.length);
if(days.length >= 1){
day2.text = [days substringFromIndex:days.length - 1];
if(days.length >= 2){
day1.text = [days substringWithRange:NSMakeRange(days.length - 2, 1)];
if(days.length >= 3){
day0.text = [days substringWithRange:NSMakeRange(days.length - 3, 1)];
}
}
}
NSString *hours = [NSString stringWithFormat:@"%d",(diffInt%86400)/3600];
hour0.text = @"0";
hour1.text = @"0";
NSLog(@"Hours Length: %d",hours.length);
if(hours.length >= 1){
hour1.text = [hours substringFromIndex:hours.length - 1];
if(hours.length >= 2){
hour0.text = [hours substringWithRange:NSMakeRange(hours.length - 2, 1)];
}
}
NSString *minutes = [NSString stringWithFormat:@"%d",((diffInt%86400)%3600)/60];
minute0.text = @"0";
minute1.text = @"0";
NSLog(@"Minutes Length: %d",minutes.length);
if(minutes.length >= 1){
minute1.text = [minutes substringFromIndex:minutes.length - 1];
if(minutes.length >= 2){
minute0.text = [minutes substringWithRange:NSMakeRange(minutes.length - 2, 1)];
}
}
最佳答案
如果您知道 2 个日期之间的时间(以秒为单位)(您的 NSTimeInterval),那么您可以轻松地将其转换为格式为 days:hours:mins:secs 的字符串,如下所示。
- (NSString*)secsToDaysHoursMinutesSecondsString:(NSTimeInterval)theSeconds {
div_t r1 = div(theSeconds, 60*60*24);
NSInteger theDays = r1.quot;
NSInteger secsLeftFromDays = r1.rem;
div_t r2 = div(secsLeftFromDays, 60*60);
NSInteger theHours = r2.quot;
NSInteger secsLeftFromHours = r2.rem;
div_t r3 = div(secsLeftFromHours, 60);
NSInteger theMins = r3.quot;
NSInteger theSecs = r3.rem;
NSString* days;
if (theDays < 10) { // make it 2 digits
days = [NSString stringWithFormat:@"0%i", theDays];
} else {
days = [NSString stringWithFormat:@"%i", theDays];
}
NSString* hours;
if (theHours < 10) { // make it 2 digits
hours = [NSString stringWithFormat:@"0%i", theHours];
} else {
hours = [NSString stringWithFormat:@"%i", theHours];
}
NSString* mins;
if (theMins < 10) { // make it 2 digits
mins = [NSString stringWithFormat:@"0%i", theMins];
} else {
mins = [NSString stringWithFormat:@"%i", theMins];
}
NSString* secs;
if (theSecs < 10) { // make it 2 digits
secs = [NSString stringWithFormat:@"0%i", theSecs];
} else {
secs = [NSString stringWithFormat:@"%i", theSecs];
}
return [NSString stringWithFormat:@"%@:%@:%@:%@", days, hours, mins,secs];
}
关于objective-c - 倒数日子 - iPhone倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177729/
为什么不倒计时? var container = document.getElementById('dl'); var seconds = 10; var timer; function count
我已经在网上搜索过,但所有现成的网站都是您指定日期的地方,它会倒计时到该日期。我需要的是可以简单地从“27 分 43 秒”(以这种格式)倒计时到 0,从他们登陆页面时开始,有人有可用的片段吗? 最佳答
我正在使用this plugin进行倒计时。在他的例子中,他正在倒计时澳大利亚国庆日 $(function () { var countdownDate = new Date(); count
所以我是 Perl 的新手,我发现我想编写一个倒计时代码,并且真的很挣扎,我在这里找到了一些代码 http://www.perlmonks.org/bare/?node_id=407922并试图弄清楚
我正在尝试创建倒计时。基本上我有客户下订单的时间(以日期时间格式存储在数据库中,例如“2014-08-14 12:52:09”),我也有完成订单所需的时间(其中要么是小时和分钟“1”45”,要么只是分
我正在使用 Flask 构建一个游戏,其中每一轮都有倒计时时间来完成这一轮。我希望时间值每秒减少 1 并在不重新加载页面的情况下进行更新。倒计时本身使用 time.sleep 等待 1 秒,然后将生成
您将获得一个名为 start_num 的整数。编写一段代码,从start_num开始倒计时到1,倒计时结束后,打印出“Liftoff!”。 我不确定如何执行此操作并一直陷入困境。 这是我在问题开始时提
我想为我的 Android 程序实现一个 java 倒计时器,但我不知道该怎么做。它应该从 60 到 0 计算时间,最后程序应该结束,但用户需要查看还剩多少时间,因此计时器应该始终可见。我设法实现了一
我正在使用 jquery_countdown (http://keith-wood.name/countdown.html) 设置 60 分钟计时器。我知道我可以在时间到期时运行回调函数,但是有没有办
所以我在这个网站上找到了这段代码,但我想更改它的一件事,但我似乎无法弄清楚,所以我需要一些帮助。我想要做的是删除日期中的年份,以便倒计时对应于它所在的年份。 var end = new Date('
所以,我有以下(秒倒计时)井然有序。但!我也尝试在倒计时中添加小时和分钟。理想情况下保持相同的结构,并且只使用纯 JS。我希望输出是: 本次促销事件还剩 X 小时、X 分钟和 X 秒! var cou
最近我正在做电子图书馆的项目,需要为用户阅读的每本书提供倒计时器。 例如,在我的数据库中,“用户”有“100 秒”的信用时间来阅读这本书,并且只有当他们阅读这本书时,时间才会减少。 这是我的代码: g
我有一个倒计时脚本,可以获取实时时间并将其从设定时间中减去。除了除非您刷新页面否则它不会更新这一事实之外,这一切都有效。我的函数底部的 setInterval 指示函数每隔一秒运行一次,但它似乎没有这
在已经运行了几次代码之后,我想再次运行我的代码(倒计时)。 Javascript: var btn1 = document.getElementById('btn1'); function timer
如何让这个倒计时在刷新时不重置倒计时?就像当我单击刷新时它应该继续倒计时而不是再次重置倒计时,从“14d、22h、23m、32s”(刷新)继续到“14d、22、59m、59s”。 通过一些研究我发现我
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我是 python 的新手,正在尝试运行一个函数,该函数将在给定一个变量的情况下倒计时到零,然后倒计时到原始变量。输出应如下所示: >>> functionname(5) 5, 4, 3, 2, 1,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我的目标很简单: 在我的 xml 文件中,我有一个名为:textView2 的 TextView 。 我需要的是一个倒计时,从 15 倒计时到 0,每过一秒,textview 就会更新(例如:15,1
我想要完成的是实时更新的倒计时...像这样: 6 天(只是几天) 12 小时(1 天内的几个小时) 59 分钟(1 小时内仅几分钟) 59 秒(1 分钟内的几秒) 实现此目的的最佳方法? 最佳答案 您
我是一名优秀的程序员,十分优秀!