【问题标题】:Count down by seconds to a specific weekday以秒为单位倒计时到特定的工作日
【发布时间】:2011-11-13 17:06:13
【问题描述】:

我正在尝试编写一个倒计时到一周中的特定日期。 我想显示当前工作日和到星期三的剩余秒数。

我可以编写第一部分的代码,但即使在寻找解决方案半天后,我也无法处理第二部分。

这就是我现在拥有的:

- (void)viewDidLoad {
    [countdownLabel setFont:[UIFont fontWithName:@"Helvetica" size:128.0]];
    countdownLabel.text = @"What day ist it?";
    [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

-(void)updateLabel {
    NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [theDateFormatter setDateFormat:@"EEEE"];
    countdownLabel.text =  [theDateFormatter stringFromDate:[NSDate date]];
}

【问题讨论】:

    标签: objective-c ios cocoa-touch time nsdate


    【解决方案1】:

    看看 NSDate 和 NSDateFormatter 类,它们有丰富的例程来做这类事情。

    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDateFormatter * df = [NSDateFormatter dateFormatFromTemplate:@"yyyy-MM-dd-HH:mm" options:0 locale:usLocale];
    NSDate * newyear = [df dateFromString:@"2012-01-01-00:00"];
    NSTimeInterval seconds = [newyear timeIntervalSinceDate:[NSDate date]];
    

    尚未尝试编译上述内容,但它应该会给您几秒钟的时间。您应该能够查看 NSDateFormatter 文档以轻松找到一周中的哪一天。

    【讨论】:

    • 谢谢,我已经这样做了。我的问题是,我将能够编写明年新年的倒计时。但是因为我想倒计时到一周中的某一天,从一周中的任何一天开始,我迷路了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2019-08-12
    • 2013-06-15
    相关资源
    最近更新 更多