个人感觉,比用NSDateComponents 的方式简洁。简单测试了下,没有问题。大家测试有问题的话,请反馈一下。
NSDate* oldDate = [NSDate date];
NSTimeInterval interval = oldDate.timeIntervalSince1970;NSDate* newDate = [NSDate dateWithTimeIntervalSince1970:interval-fmod(interval,60*60*24)];
用NSDateComponents的方式:
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:date];
NSDate* dateOnly = [calendar dateFromComponents:components];
原文地址:http://blog.csdn.net/gaoyp/article/details/40784475