0

イベントの日付を確認する必要があります。これは、現在の日付から 60 日後までの間にある必要があります。以下のコードが使用されていますが、正しく動作していません。サーバーから「2012-04-14T16:50:02Z」のようなイベント文字列を取得していることに注意してください。

// current date
double currDateInMilliSecs = [NSDate timeIntervalSinceReferenceDate] * 1000;    
NSLog(@"currDateInMilliSecs: %f", currDateInMilliSecs);

// sixty days
double sixtydaysvalue = 60.0 * 24.0 * 3600.0 * 1000.0;
NSLog(@"sixtydaysvalue: %f", sixtydaysvalue);
// add current date + sixt days
double sixtyDaysMilliSecsFromCurrDate = currDateInMilliSecs + sixtydaysvalue;
NSLog(@"sixtyDaysMilliSecsFromCurrDate: %f", sixtyDaysMilliSecsFromCurrDate);

// check does the event date between current date + 60 days
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//[df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
//[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// [eventDict objectForKey:@"begin_at"] gives date string like this "2012-04-14T16:50:02Z"  for ex.
NSDate *eventdate = [df dateFromString:[eventDict objectForKey:@"begin_at"]];

NSTimeInterval nowSinceEventDate = [eventdate timeIntervalSince1970];
NSLog(@"nowSinceEventDate: %f", nowSinceEventDate);
double eventDateInMilliSecs = nowSinceEventDate * 1000;
NSLog(@"eventDateInMilliSecs: %f", eventDateInMilliSecs);

// this is not working as expected    
if ( eventDateInMilliSecs<sixtyDaysMilliSecsFromCurrDate && eventDateInMilliSecs>currDateInMilliSecs )
{


}
else
{
}

何か助けてください。

4

3 に答える 3

1

これを試して:

NSString *dateString = @"2012-04-14T16:50:02Z";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *eventDate = [formatter dateFromString:dateString];

NSTimeInterval nowSinceEventDate = [eventDate timeIntervalSince1970];
NSLog(@"interval = %f", nowSinceEventDate);

アップデート:

NSString *dateString = @"2012-05-21T16:50:02Z";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *eventDate = [formatter dateFromString:dateString];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:60];

NSDate *minDate = [NSDate date];
NSDate *maxDate = [gregorian dateByAddingComponents:components toDate:minDate options:0];

NSLog(@"eventDate interval = %f", [eventDate timeIntervalSince1970]);
NSLog(@"minDate interval = %f", [minDate timeIntervalSince1970]);
NSLog(@"maxDate interval = %f", [maxDate timeIntervalSince1970]);

BOOL isBetween = (([eventDate compare:minDate] == NSOrderedDescending) && ([eventDate compare:maxDate] == NSOrderedAscending));
NSLog(@"isBetween = %d", isBetween);
于 2012-04-19T12:04:32.687 に答える
1

これを試して

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss SSS"];

于 2012-04-19T12:10:04.240 に答える
0
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
// [eventDict objectForKey:@"begin_at"] gives "2012-04-14T16:50:02Z"
NSDate *eventdate = [df dateFromString:[eventDict objectForKey:@"begin_at"]];

[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

ミリ秒のSSS

于 2012-04-19T12:01:53.593 に答える