将uipickerdate与当前日期进行比较

时间:2013-04-06 02:23:29

标签: ios objective-c xcode uipickerview uidatepicker

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

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范围?

谢谢,

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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

我没有测试过这些。

答案 2 :(得分:2)

只需添加:

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

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

如果要计算两个日期之间的间隔,请使用此方法

- (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

中添加此语句
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

添加功能以在发布通知时提供通知

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

在类的dealloc函数中删除观察者的通知

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

答案 3 :(得分:2)

您也可以使用NSDate的compare:方法

进行以下操作
 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,希望它会帮助你。