从当前日期减去7天 [英] Subtract 7 days from current date
本文介绍了从当前日期减去7天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看来我不能从当前日期减去7天.这就是我的做法:
It seems that I can't subtract 7 days from the current date. This is how i am doing it:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-7];
NSDate *sevenDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:[NSDate date] options:0];
SevenDaysAgo与当前日期具有相同的值.
SevenDaysAgo gets the same value as the current date.
请帮助.
在我的代码中,我忘了用正确的日期替换获取当前日期的变量.因此,上面的代码可以正常工作.
In my code I forgot to replace the variable which gets the current date with the right one. So above code is functional.
推荐答案
使用dateByAddingTimeInterval方法:
use dateByAddingTimeInterval method:
NSDate *now = [NSDate date];
NSDate *sevenDaysAgo = [now dateByAddingTimeInterval:-7*24*60*60];
NSLog(@"7 days ago: %@", sevenDaysAgo);
输出:
7 days ago: 2012-04-11 11:35:38 +0000
希望有帮助
这篇关于从当前日期减去7天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文