2015-10-16 49 views
-3

在此image中有一個彈出窗口,其中有一些數字。前8位數字爲YYYYMMDD格式。我必須驗證,如果輸入的日期是過去(較早)的日期,那麼應該在文本框下面的彈出窗口中顯示一條消息,說明「過去的日期」。如果日期是當前日期或未來日期,那麼它應該移動到下一個viewController。我應該如何驗證此UI的日期格式?

+0

不要強制用戶以特定格式輸入日期字符串。有單獨的文本字段,或使用選擇器視圖。 –

回答

-1
NSString *strTextField = @"20151012122"; 
NSString *strDate = [strTextField substringToIndex:8]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyyMMdd"]; 
NSDate *pDate = [dateFormat dateFromString:strDate]; 

NSDate *pCurrentDate = [NSDate date]; 

if ([pDate compare:pCurrentDate] == NSOrderedDescending) { 
    NSLog(@"past date, pDate is less than current date."); 
} 
else { 
    NSLog(@"should move to the next viewController"); 
} 
+0

日期格式化程序應該是'yyyyMMdd'。大寫確實很重要。特別是在'天'部分。有關更多詳細信息,請參閱http://waracle.net/iphone-nsdateformatter-date-formatting-table/ – psci