开发者

Working out "Today" and "Yesterday" from NSDate?

开发者_Go百科I want to present a date to the user of my app as "Today", "Yesterday" or as a formatted date (i.e. 27/05/2011). Is there a quick way to get "Today" or "Yesterday" based on a given NSDate? If not I can write the code myself, I am just curious if I am overlooking some simpler way than working out remaining hours manually.


If you just want to present date to your user, there is an option in NSDateFormatter right for that.
- (void)setDoesRelativeDateFormatting:(BOOL)b

Take a look at documentation for more information.


Check out this similar question: Compare NSDate for Today or Yesterday.

You make NSDate objects from today and yesterday, and then compare the first 10 characters of their description to the NSDate you're unsure of.


From the Date and Time Programming Guide:

NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;

tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
[today release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜