2013-04-06 75 views
1

下面是我的两个日期选择器的代码,我需要使用if条件或开关将它们与当前日期进行比较。将uipickerdate与当前日期进行比较

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
dateFormatter.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter.timeStyle=NSDateFormatterShortStyle; 
dateFormatter.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date]; 
NSLog(@"Start time is %@",dateTimeString); 


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init]; 
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter2.timeStyle=NSDateFormatterShortStyle; 
dateFormatter2.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date]; 
NSLog(@"End time is %@",dateTimeString2); 

在我的.h

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime; 
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime; 

我该怎么办呢?我是否需要将它们存储到NSDate或NSTime中,因为我只需要时间?

另外,Objective C中有If范围吗?

感谢,

+1

我在代码中看不到任何日期选择器。你在说什么日期选择器? – matt 2013-04-06 02:27:36

+0

谢谢@matt检查我的编辑并注意我的.h,starttime.date和endtime.date实际上是我的日期选择器 – user1949873 2013-04-06 02:52:43

+0

那么,我的答案仍然存在。 – matt 2013-04-06 03:04:34

回答

2

只需添加这

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; 

添加函数以迎合通知当它被张贴

-(void)applicationEnteredBackground 
{ 
    //do all the above steps here when you want. 
} 

而且在通知

-(void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
+0

优秀的直接答案!你知道我该如何将它链接到'AppDelegate'的'DidEnterBackground'方法? – user1949873 2013-04-06 04:16:20

+0

@ user1949873意味着你想要什么?你想使用任何值吗? – iSpark 2013-04-06 10:19:01

+1

好吧,让我直接得到这个,当应用程序进入后台状态时,你想要做这一切吗?好吧,看看我的编辑。 – satheeshwaran 2013-04-06 12:16:02

2

的唯一日期,我在你的代码中看到的是startTime.dateendTime.date。我不知道你在哪里得到这些,但他们大概是NSDates。另一方面,当前日期是[NSDate date]。因此,您可以使用NSDate的compare:方法或任何其他几种比较方法进行比较(请参阅NSDate文档)。

+0

你能给我举个例子吗? – user1949873 2013-04-06 03:09:01

+0

'NSComparisonResult res = [[NSDate date] compare:startTime。日期]' – matt 2013-04-06 03:10:29

2

NSDate执行isEqualToDate:它测试两个日期是否相同直到微秒。它还实现了earlierDate:laterDate:,您可以使用它来构建范围检查。

你也可以建立一些助手,让你使用timeIntervalSinceDate:来控制平等检查的灵敏度。没有日期范围对象,但您也可以在支票之间添加一张支票...

@interfce NSDate (Comparison) 

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance; 
- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance; 

@end 

@implementation NSDate (Comparison) 

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance { 

    NSTimeInterval difference = [self timeIntervalSinceDate:aDate]; 
    return fabs(difference) < tolerance; 
} 

- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance { 

    NSTimeInterval startDifference = [self timeIntervalSinceDate:start]; 
    if (startDifference < tolerance) return NO; 

    NSTimeInterval endDifference = [end timeIntervalSinceDate:self]; 
    if (endDifference < tolerance) return NO; 

    return YES; 
} 

@end 

我还没有测试过这些。如果你想之间两个日期使用此方法

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate 


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0) 
{ 
    //start time greater than today 
} 

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0) 
{ 
    //start time less than today 
} 

else 
{ 
    //both dates are equal 
} 

为了知道什么时候该应用已经进入后台使用的通知在viewDidLoad中添加此语句来计算区间

if ([[NSDate date] isEqualToDate:startTime.date]) { 
NSLog(@"currentDate is equal to startTime"); 
} 

if ([[NSDate date] isEqualToDate:endTime.date]) { 
NSLog(@"currentDate is equal to endTime"); 
} 

+0

NSDate有比较方法,所以为什么重新发明那个轮子? (不要批评,我想我可能会错过一些东西) – matt 2013-04-06 03:04:12

+0

这是一个有效的问题。我添加了测试,以包含在我认为OP需要的范围内,以及降低测试灵敏度的想法。 – danh 2013-04-06 04:44:27

2

你也可以像下面使用的NSDate的比较类中删除观察者的dealloc功能:方法,

NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"MM-dd-yyyy"]; 
NSDate* date1 = [df dateFromString:@"12-23-2046"]; 

NSDate *date2 = [NSDate date]; 

if ([date1 compare:date2] == NSOrderedSame){ 
    NSLog(@"Same"); 
} 
if ([date2 compare:date1]==NSOrderedAscending) { 
    NSLog(@"AScending"); 
} 
if ([date2 compare:date1]== NSOrderedDescending) { 
    NSLog(@"Descending"); 

} 

起飞你的startTime,endTime而不是date1,希望它能帮助你。

相关问题