目前,我可以使用以下代码
获取iPhone的当前日期NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString* currentDateStr = [formatter stringFromDate:date];
NSLog(@"User's current Date:%@",currentDateStr);
但我希望从当前日期开始接下来的15天,我怎么能得到它?
答案 0 :(得分:6)
试试这个:
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 15;
NSDate* newDate = [calendar dateByAddingComponents: components toDate: self.date options: 0];
答案 1 :(得分:1)
NSDate *todayDate = [NSDate date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:+15];
NSDate *afterfifteenDays = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:todayDate options:0];
NSLog(@"todayDate: %@", todayDate);
NSLog(@"afterfifteenDays: %@", afterfifteenDays);
答案 2 :(得分:-1)
试试这个:
NSTimeInterval totalOffset = /*time to add - in seconds */
NSDate *today = [NSDate date];
NSDate *endWithTZ = [today dateByAddingTimeInterval:totalOffset];
答案 3 :(得分:-2)
可能有更好的方式(我正在看这个问题,看看是否有),但这是我目前使用的:
+ (NSDate *)offsetFromDate:(NSDate *)fromDate withDays:(int)days
{
NSTimeInterval timeInterval = (days * 86400);
NSDate *date = [[NSDate alloc] initWithTimeInterval:timeInterval sinceDate:fromDate];
return date;
}
如果你谷歌获得一天中的秒数,你将获得值86400。
答案 4 :(得分:-2)
NSLog(@"%@", [date dateByAddingTimeInterval:15*24*60*60]);