按秒计算到特定的工作日

时间:2011-11-13 17:06:14

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

我正在尝试将倒计时编码到本周的特定日期。 我想显示当前的工作日和周三的剩余秒数。

我能够对第一部分进行编码,但即使在寻找解决方案的半天后,我也无法处理第二部分。

这就是我现在所拥有的:

- (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]];
}

1 个答案:

答案 0 :(得分:0)

同时查看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文档以轻松找到一周中的那一天。