我正在使用下面的代码仅从NSDate中提取日期.我究竟做错了什么?
NSDate* now = [NSDate date]; NSLog(@"Now Date %@",now); NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"dd-MM-yyyy"; NSString *stringDate = [format stringFromDate:now]; NSDate *todaysDate = [format dateFromString:stringDate]; NSLog(@"Today's Date without Time %@",todaysDate);
日志:
2014-06-21 12:27:23.284 App[69727:f03] Now Date 2014-06-21 19:27:23 +0000 2014-06-21 12:27:23.285 App[69727:f03] Today's Date without Time 2014-06-21 07:00:00 +0000
我为什么得到:07:00 00 00 00结束?
我希望以下列格式获得NSDate:
2014-06-21 00:00:00 +0000
0,时间,秒等并不重要.
解决方法
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDate *now = [[NSDate alloc] init]; NSString *theDate = [dateFormat stringFromDate:now];
应该管用