iOS开发之获得当前时间的前几天,几个月,几年
2017-02-06 00:00
417 查看
通过传入year 和 month 和 day 得出currentTime 的前几天 前几月 钱
-(NSDate *)beforYear:(NSInteger)year andBeforMonth:(NSInteger)month andBeforDay:(NSInteger)day andCurrentTime:(NSDate *)currentTime{
//得到当前的时间
//NSDate * mydate = [NSDate date];
// NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
// NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
// NSLog(@"---当前的时间的字符串 =%@",currentDateStr);
// 先定义一个遵循某个历法的日历对象 (历法就是 推算日月星辰之运行以定岁时节候的方法 就看成日历好了)
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = nil;
comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:currentTime];
NSDateComponents *adcomps = [[NSDateComponents alloc] init];
[adcomps setYear:year];
[adcomps setMonth:month];
[adcomps setDay:day];
NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:currentTime options:0];
// NSString *beforDate = [dateFormatter stringFromDate:newdate];
// NSLog(@"---前2年 =%@",beforDate);
return newdate;
}
-(NSDate *)beforYear:(NSInteger)year andBeforMonth:(NSInteger)month andBeforDay:(NSInteger)day andCurrentTime:(NSDate *)currentTime{
//得到当前的时间
//NSDate * mydate = [NSDate date];
// NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.S"];
// NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
// NSLog(@"---当前的时间的字符串 =%@",currentDateStr);
// 先定义一个遵循某个历法的日历对象 (历法就是 推算日月星辰之运行以定岁时节候的方法 就看成日历好了)
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = nil;
comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:currentTime];
NSDateComponents *adcomps = [[NSDateComponents alloc] init];
[adcomps setYear:year];
[adcomps setMonth:month];
[adcomps setDay:day];
NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:currentTime options:0];
// NSString *beforDate = [dateFormatter stringFromDate:newdate];
// NSLog(@"---前2年 =%@",beforDate);
return newdate;
}
相关文章推荐
- ios 开发中获得当前时间的 年月日,时分秒
- ios开发 获取系统时间 取得当前的年月日,当前的时分秒获得,周几和星期几获得
- ios 开发中获得当前时间的 年月日,时分秒
- ios写程序的总结(获得归档路径,获得当前时间)
- IOS 获得当前系统时间(当前时区)
- 时间戳 获得当前时间 -iOS
- 本篇文章写写得到推送的时候,获得当前显示vc的方法,,ios开发如何获得当前vc
- iOS开发-NSDate获取当前时区时间
- ios开发——代码实现获得手机系统的名字和版本号&当前网络状态
- iOS开发-基础:获取系统当前日期,断点输出时间
- iOS开发 当前时间 时间戳 转换
- iOS开发 NSDate 获取系统当前时间
- IOS开发—对当前屏幕闲置时间计时
- (礼拜一log)前端开发:jsp中获得当前时间
- ios 获得当前的系统时间和日期
- [iOS]获得当前系统时间(当前时区)
- iOS原生开发获取当前设备日期与时间
- (0035) iOS 开发之获得当前设备的IP(分IPv4 & IPv6)
- (前端开发)java获得当前时间和第二天时间
- IOS开发之获得当前的网速