gpt4 book ai didi

ios - NSTimer 中的 NSDate 格式化程序

转载 作者:行者123 更新时间:2023-11-29 13:11:21 25 4
gpt4 key购买 nike

所以我有一个 NSTimer,我使用日期格式化程序来暂停和播放计时器,但是当我启动计时器时,它从 07:00:00 开始,而我希望它从 00:00:00 开始。

我的定时器代码是

-(void) updateTimer {

NSDate *currentDate = [NSDate date];

NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
//[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

NSString *timeString = [dateFormatter stringFromDate:timerDate];
hhmmssLabel .text = timeString;
pauseTimeInterval = timeInterval;

}

-(void)pauseTimer {

[tickerTimer invalidate];
tickerTimer = nil;
[self updateTimer];
}

-(void) unPauseTimer {

startDate = [NSDate date] ;
startDate = [startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))];
tickerTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}

我假设它与时区有关,所以我将其注释掉但没有帮助。我无法让它在 00:00:00 开始

最佳答案

我认为您可以稍微简化代码。您的要求并不太复杂(您只是处理间隔而不是绝对日期),所以可以使用一些东西 like this :

NSDate * currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];

// NSTimeInterval is in measured in Seconds
NSInteger hours = ( (NSInteger) timeInterval / 3600.0 );
NSInteger mins = ( (NSInteger) (timeInterval / 60) % 60 );
NSInteger secs = ( (NSInteger) timeInterval % 60.0 );

NSString * timeString = [NSString stringWithFormat:%02u:%02u:%02u, hours, mins, secs];

否则尝试查看this answer .也许您需要 @"HH:mm:ss"(24 小时制)或 [NSTimeZone timeZoneWithName:@"UTC"]];

关于ios - NSTimer 中的 NSDate 格式化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com