有时候在开发中会遇到一些需求,比如:在一个选择器,需要我们去设置它的一个时间最大选择值,从而控制它的一个时间选择段。
那,我们可能会需要到一个问题,如果得到当前时间之后n天的日期呢?
直接上代码:
/**
得到当前时间之后N天的日期
@param day N天
@return return value description
*/
- (NSDate *)getTimeAfterNowWithDay:(NSInteger)day
{
NSDate *nowDate = [NSDate date];
NSDate *theDate;
if(day!=0)
{
NSTimeInterval oneDay = 24*60*60*1; //1天的长度
theDate = [nowDate initWithTimeIntervalSinceNow: +oneDay*day ];
//or
//theDate = [nowDate initWithTimeIntervalSinceNow: -oneDay*day ];
}
else
{
theDate = nowDate;
}
return theDate;
}
欢迎大家访问我的GitHub
GitTub:https://github.com/JnKindle