我需要知道两个NSDate实例是否都来自同一天。有没有比获取NSDateComponents并比较日/月/年更简单/更好的方法? 最佳答案 如果您的目标是iOS8(和OSX10.9)或更高版本,那么Joe'sanswer只是为此目的使用NSCalendar中的新方法是一个更好的解决方案:-[NSCalendarisDate:inSameDayAsDate:]对于iOS7或更早版本:NSDateComponents是我的首选。像这样的东西怎么样:-(BOOL)isSameDayWithDate1:(NSDate*)date1date2
我需要知道两个NSDate实例是否都来自同一天。有没有比获取NSDateComponents并比较日/月/年更简单/更好的方法? 最佳答案 如果您的目标是iOS8(和OSX10.9)或更高版本,那么Joe'sanswer只是为此目的使用NSCalendar中的新方法是一个更好的解决方案:-[NSCalendarisDate:inSameDayAsDate:]对于iOS7或更早版本:NSDateComponents是我的首选。像这样的东西怎么样:-(BOOL)isSameDayWithDate1:(NSDate*)date1date2
在没有其他信息的情况下,如何获取NSDate对象的年/月/日?我意识到我可以用类似的东西来做到这一点:NSCalendar*cal=[[NSCalendaralloc]init];NSDateComponents*components=[calcomponents:0fromDate:date];intyear=[componentsyear];intmonth=[componentsmonth];intday=[componentsday];但是对于像获取NSDate的年/月/日这样简单的事情来说,这似乎很麻烦。还有其他解决办法吗? 最佳答案
在没有其他信息的情况下,如何获取NSDate对象的年/月/日?我意识到我可以用类似的东西来做到这一点:NSCalendar*cal=[[NSCalendaralloc]init];NSDateComponents*components=[calcomponents:0fromDate:date];intyear=[componentsyear];intmonth=[componentsmonth];intday=[componentsday];但是对于像获取NSDate的年/月/日这样简单的事情来说,这似乎很麻烦。还有其他解决办法吗? 最佳答案