在NSDate没有其他信息的情况下,如何获得对象的年/月/日?我意识到我可以用类似的东西做到这一点:
NSCalendar *cal = [[NSCalendar alloc] init];
NSDateComponents *components = [cal components:0 fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];
Run Code Online (Sandbox Code Playgroud)
但这对于像NSDate年/月/日这样简单的事情来说似乎有很多麻烦.还有其他解决方案吗?
如何检查一个NSDate属于今天?
我曾经使用前10个字符来检查它[aDate description].[[aDate description] substringToIndex:10]像"YYYY-MM-DD"这样返回字符串我将字符串与返回的字符串进行了比较[[[NSDate date] description] substringToIndex:10].
有更快速和/或更好的方式来检查?
谢谢.
我有一个应用程序,显示某些渡轮旅行的时间表.
如果我前往不同的时区 - 比如说落后4个小时,那么上午10点的轮渡旅行现在显示为早上6点?
我知道这与如何根据时区处理日期有关,但我无法弄清楚如何改变这种行为.
目前,这是我如何获取日期并在UILabel上显示:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"departureTime"]]];
[self.arrivalTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"arrivalTime"]]];
[dateFormatter release];
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.