我需要檢查一個事件日期,該日期應該在當前日期和從現在起的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
{
}
請幫忙嗎?
如果您需要以3:43:12:23的時間顯示毫秒,我的回答或@ vinay的答案 – Ranga 2012-04-19 12:15:46
[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