如何获得上个月的NSDate开始?

时间:2014-11-17 11:11:23

标签: ios objective-c nsdate

我想我会问重复的问题,但我无法找到解决方案。我需要得到上个月的第一天。据我所知,我必须从今天开始获取日历,减去1个月并将日期设为第1天。之后我必须转换为日期。不幸的是,我上个月的最后一天。

我使用以下代码:

NSDate *maxDate = [[NSDate alloc] init];
NSCalendar *calendarCurr = [NSCalendar currentCalendar];
NSDateComponents *components = [calendarCurr components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:maxDate];
[components setMonth:-1];
components.day = 1;

NSDate *minDate = [calendarCurr dateFromComponents:components];
NSLog(@"%@", minDate);

修改

是的,它是重复的,但该问题的答案对我没有帮助。我仍然得到30/09/2014(今天是2014年11月17日),而不是01/10/2014。我想我的问题是今天的日期。

4 个答案:

答案 0 :(得分:1)

错误的计算。 使用此代码设置Month组件。

[components setMonth:components.month -1];

答案 1 :(得分:1)

嗯,这是一种新方法:

    NSDate *today = [NSDate date];
NSDateFormatter *formatterYear = [[NSDateFormatter alloc] init];

[formatterYear setDateFormat:@"yyyy"];
NSDateFormatter *formatterMonth = [[NSDateFormatter alloc] init];
[formatterMonth setDateFormat:@"MM"];
[formatterMonth setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *yearString = [formatterYear stringFromDate:today];
NSString *monthString = [formatterMonth stringFromDate:today];
NSInteger month,year;


if ([monthString integerValue] == 1) {
     year = [yearString integerValue] -1;
    month = 12;
} else
{
    year = [yearString integerValue];
    month = [monthString integerValue]-1;

}
NSDateFormatter *exitFormatter = [[NSDateFormatter alloc] init];
[exitFormatter setDateFormat:@"yyyy-MM-dd"];
[exitFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *stringDate;
if (month < 10) {
    stringDate = [NSString stringWithFormat:@"%ld-0%ld-01",(long)year,(long)month];
} else {
    stringDate = [NSString stringWithFormat:@"%ld-%ld-01",(long)year,(long)month];
}
NSDate *firstDatePreviousMonth = [exitFormatter dateFromString:stringDate];

NSLog(@"View the date: %@",[firstDatePreviousMonth description]);

答案 2 :(得分:0)

NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:@"MM"];
NSString *stringCurrentDate = [dateFormatter1 stringFromDate:currentDate];
int monthInt= [stringCurrentDate intValue];

[dateFormatter1 setDateFormat:@"yyyy"];
NSString *dateString = [dateFormatter1 stringFromDate:currentDate];
int yearInt = [dateString intValue];

monthInt-= 1;
if (monthInt== 0) {
    monthInt= 12;
    yearInt -=1;
}

dateString = [NSString stringWithFormat:@"%@-01-%@",[@(monthInt)stringValue],[@(yearInt)stringValue]];
NSLog(@"this is previous month first date : %@",dateString);

按原样运行,显然月份的第一天将是01,所以这在这里是静态的。你可以相应改变。快乐的编码

答案 3 :(得分:-1)

试试这个:

NSDate *maxDate = [NSDate date];
NSCalendar *calendarCurr = [NSCalendar currentCalendar];
NSDateComponents *components = [calendarCurr components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitTimeZone | NSCalendarUnitSecond | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:maxDate];

[components setMonth:components.month - 1];

[components setDay:1];

NSDate *minDate = [calendarCurr dateFromComponents:components];
NSLog(@">>>>>>>>>>%@", minDate.description);