2012-04-19 125 views
0

我需要檢查一個事件日期,該日期應該在當前日期和從現在起的60天之間。下面的代碼被使用,但它不能正常工作。請注意,我收到來自服務器的事件字符串 - 「2012-04-14T16:50:02Z」。iPhone:日期轉換問題

// 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 
{ 
} 

請幫忙嗎?

+0

如果您需要以3:43:12:23的時間顯示毫秒,我的回答或@ vinay的答案 – Ranga 2012-04-19 12:15:46

+0

[iPhone:可能與當前日期和當前日期之間的60天之間的日期進行比較的問題]的重複( http://stackoverflow.com/questions/10228816/iphone-issue-comparing-a-date-between-current-date-and-sixty-days-from-current) – 2012-04-19 13:44:57

回答

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的毫秒數

+0

此問題已解決。謝謝大家。 – Getsy 2012-04-19 16:50:16

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); 

UPDATE:

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); 
+0

其實我已經這樣做了,它正在工作。我現在的問題是新的。我想比較「2012-04-14T16:50:02Z」日期應該在當前日期和加上60天之間。所以我添加了像獲取當前日期的邏輯,以毫秒爲單位,然後添加60天毫秒加上當前日期(毫秒),並將其與dateString =「2012-04-14T16:50:02Z」毫秒進行比較。但是,它並沒有按照我的需要進行比較。因爲,當前日期是356530435017.459961,加上60天是361714435017.459961,但dateString =「2012-04-14T16:50:02Z」millisecs是1336559400000.000000。任何幫助? – Getsy 2012-04-19 12:17:01

+0

請查看我在問題中更新的問題。 – Getsy 2012-04-19 12:27:25

+0

看到更新:) – demon9733 2012-04-19 15:11:16

1

試試這個

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

+0

這是正確的,如果需要顯示毫秒與時間一樣像3:43:12:23 – Ranga 2012-04-19 12:16:22

+0

請查看我在問題中更新的問題。 – Getsy 2012-04-19 12:26:47