你如何在Xcode中检测它在iOS上的日期

时间:2012-12-11 19:58:06

标签: iphone objective-c ios xcode ipad

嗨,我正在编写一个有竞争力的应用程序。

UIAlertView *alert = [[UIAlertView alloc]

                      initWithTitle:@"Icon Contest!"
                      message:@"Design a 1024 x 1024 pixels icon for Example. The best one will be on the app's icon! Send them to: example@example.com."
                      delegate:self
                      cancelButtonTitle:@"More Info"
                      otherButtonTitles:@"Yes",@"No", nil];

[alert show];

我有截止日期,并且希望应用在1月31日结束时显示不同的提醒。

3 个答案:

答案 0 :(得分:4)

[NSDate date]返回当前日期 使用NSDateComponent可以从NSDate获取月,日等详细信息。

答案 1 :(得分:2)

正如yinkou所指出的,您可以使用[NSDate date]来获取当前日期。但是,如果您在-timeIntervalSinceNow上查看NSDate方法,可能会更简单。你可以这样做:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:31];
[comps setMonth:1];
[comps setYear:2013];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
if ([date timeIntervalSinceNow] < 0.0) {
    //The date has passed
}
else {
    //The date hasn't passed
}

答案 2 :(得分:1)

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:31];
[components setMonth:1];
[components setYear:2013];

NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *releaseDate = [gregorian dateFromComponents:components];

if ([NSDate date] == releaseDate)
{
[alert show];
}