1

startTime と endTime を提供する 2 つの日付ピッカーがあります。このコードによると:

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;

そして、与えられた時間と現在の時間を比較できるこのコードがあります。バックグラウンドで使用する必要があるため、テストする最良の方法は何ですか。

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

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

そのコードかこのコードのどちらかを選択する必要があります。NSLogここでうまくいくでしょうか?

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

ありがとう

4

2 に答える 2

6
NSDate *date1;
NSDate *date2;

次に、次の比較により、どちらが早い/遅い/同じであるかがわかります。

 if ([date1 compare:date2] == NSOrderedDescending) {
 NSLog(@"date1 is later than date2");        

 } else if ([date1 compare:date2] == NSOrderedAscending) {
  NSLog(@"date1 is earlier than date2");

 } else {
NSLog(@"dates are the same");
 }
于 2013-04-10T05:53:20.007 に答える
1

NSComparator を使用して、2 つのオブジェクトの値を比較できます

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *strDate;
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
strDate = [formatter dateFromString:@"2012-12-07 08:43:31"];
endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];

NSLog(@"%d",[strDate compare:endDate]);
if([strDate compare:endDate] < 0)
    NSLog(@"Enddate bigger than serdate");
else if([strDate compare:endDate] > 0)
    NSLog(@"strdate bigger than enddate");
else
    NSLog(@"Both Are Identicle");
于 2013-04-10T04:29:52.480 に答える