获取下一个日期和前一天的日期

时间:2012-12-25 08:17:03

标签: iphone ios nsdate nsdateformatter

我基本上想要将特定日期(x)转换为前一天日期(x - 1天)以及第二天日期(x + 1天)。我正在使用以下代码:

NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];

但是我的日期(x)是NSString格式的,我需要在应用上面的代码之前将NSString(myDateString)转换为NSDate(myDate)。 我有一个NSString包含日期格式MM-dd-yyyy。 对于转换我使用以下代码,但我得到了荒谬的价值。

 NSLog(@"myDateString=%@",myDateString);//output:myDateString=10-25-2012//all correct
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSDate *myDate=[formatter dateFromString:myDateString];
NSLog(@"myDate=%@",myDate);//output:myDate=2012-10-24 18:30:00 +0000 // I only want the date to be shown // and why has the format changed
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(@"datePlusOneDay=%@",datePlusOneDay);//output:datePlusOneDay=2012-10-25 18:30:00 +0000// I only want the date to come , not time // and why has the format changed

稍后我需要将NSDate转换为NSString

NSString *curentString=[formatter stringFromDate:datePlusOneDay];
NSLog(@"curentString=%@",curentString); //output:curentString=10-26-2012

同样,我也希望得到上一个日期。 请帮帮我们!!和ho ho MERRY CHRISTMAS !!

6 个答案:

答案 0 :(得分:9)

执行:componets.day = 1获取下一个-1前一天。

NSDate *date = [NSDate date]; // your date from the server will go here.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"newDate -> %@",newDate);

答案 1 :(得分:4)

以下代码可以为您提供上一个日期:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-1]; // replace "-1" with "1" to get the datePlusOneDay
NSDate *dateMinusOneDay = [gregorian dateByAddingComponents:offsetComponents toDate:myDate options:0];

Merry Xmas:)

答案 2 :(得分:1)

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];

[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];

答案 3 :(得分:1)

在以前的日期尝试这个简单的解决方案: -

NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)];
NSLog(@"datePlusOneDay=%@",datePlusOneDay);

答案 4 :(得分:1)

Swift 5解决方案

let calendar = Calendar.current
calendar.date(byAdding: .day, value: -2, to: Date())

答案 5 :(得分:0)

previousDay的功能

-(void)previousDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =-1;
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    EndingDate = [dateFormat stringFromDate:sevenDays];

}

第二天的功能

-(void)nextDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =1;
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    EndingDate = [dateFormat stringFromDate:sevenDays];

}