NSTimer 倒计时标签

发布于 2024-11-26 13:59:57 字数 81 浏览 2 评论 0原文

它将不断倒计时并更新当前时间。任何人都知道如何解决这个问题?我不明白 NStimer 如何识别这样的日期格式: 20110803 23:59:59

It will keep counting down and updating the current time. Anyone has any idea how to approach this problem? I don't see how the NStimer will recognize the date format like this:
20110803 23:59:59

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨夜星沙 2024-12-03 13:59:57

查找 NSDateFormatter。您可以指定格式字符串 (-[setDateFormat:]),以便将 NSDateNSString 转换为 AND。

当转换回倒计时视图时,您可能需要使用 NSDateComponentsNSCalendar 获取倒计时标签所需的部分(而不是NSDateFormatter)。您可以执行以下操作:

NSDateComponents *countdown = [[NSCalendar currentCalendar] 
    components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
      fromDate:[NSDate date] 
        toDate:expiration 
       options:0];

这将为您计算所有单位差异(相对于设备当前配置的日历设置)。

您可以通过 [countdown day][countdown hour] 等调用取回组件。

Look up NSDateFormatter. You can specify a format string (-[setDateFormat:])that lets you convert to AND from NSDate and NSString.

When converting back to your countdown view, you may want to use NSDateComponents and NSCalendar to get the pieces you need for your countdown label (instead of NSDateFormatter). You could do something like:

NSDateComponents *countdown = [[NSCalendar currentCalendar] 
    components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
      fromDate:[NSDate date] 
        toDate:expiration 
       options:0];

This will calculate all the unit differences for you (with respect to the device's currently configured calendar settings).

You can get the components back with a call like [countdown day] or [countdown hour].

一场春暖 2024-12-03 13:59:57

您可以通过像这样使用 NSDateFormatter 从字符串中获取 NSDate 对象来轻松获取日期。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd' 'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:your_date_string];

You can get the date easily by using NSDateFormatter like this to get a NSDate object from your string.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd' 'HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormatter dateFromString:your_date_string];
旧梦荧光笔 2024-12-03 13:59:57

您可以使用 NSDateformatter 类将日期从 NSString 转换为 NSDate。

NSString *dateString = @"20110803 23:59:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];  

现在,为了倒计时,您使用 NSTimer 类每隔一秒触发一次,然后您可以通过以下方式获取当前日期和 date 变量之间的秒数差异:只需使用timeIntervalSinceDate

NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:yourDate];

现在您已经得到了以秒为单位的差异,您可以将其转换为您想要显示的任何格式。

You could convert the date from NSString to NSDate by using the NSDateformatter class.

NSString *dateString = @"20110803 23:59:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];  

Now, in order to count down, you use the NSTimer class to fire every one second, then you can get the difference in seconds between the current date and your datevariable by simply use timeIntervalSinceDate.

NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:yourDate];

Now that you have the difference in seconds, you convert it to whatever format you wish to display.

一生独一 2024-12-03 13:59:57

我没有深入研究这个问题,所以很抱歉,如果我的回答有点不恰当。

在这种情况下我会使用 NSDate。使用 NSDateFormatter 我会将 xml 文本数据转换为 NSDate。当 NSTimer 的每个刻度正确时,更改 NSDate 的值。
还有一些代码,因为 NSDateFormatter 的符号有时会造成混乱:

    NSString *xmlDateFormat = @"yyyyMMdd HH:mm:ss";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:xmlDateFormat];
    NSDate *yourDate = [dateFormatter dateFromString:timeStringFromXml];

这是用于从 xml 读取的。对于在勾选时更改 NSDate 的值,您可以查看 NSDate 的文档。为了显示标签中的值,我将通过算术计算每个值并在随意的 NSString 的帮助下显示它:

   [NSString stringWithFormat:@"%dday %dh %dm %ds", days, hours, minutes, seconds];

I didn't drill down into this problem deeply, therefore sorry, if my answer is a bit unappropriate.

In this case I would use NSDate. Using NSDateFormatter I would convert text data of xml to NSDate. Changing the value of NSDate, when every tick of NSTimer, properly.
And a bit of code, 'cause the symbols of NSDateFormatter sometimes create mess:

    NSString *xmlDateFormat = @"yyyyMMdd HH:mm:ss";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:xmlDateFormat];
    NSDate *yourDate = [dateFormatter dateFromString:timeStringFromXml];

This is for reading from xml. For changing value of NSDate when tick you can see the doc of NSDate. And for display the value in label I would arithmetically calculate every value and show it with the help of casual NSString:

   [NSString stringWithFormat:@"%dday %dh %dm %ds", days, hours, minutes, seconds];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文