按钮单击显示下一个/上一个日期

时间:2014-10-06 07:21:53

标签: ios iphone

我有2个按钮,一个用于增加当前日期,一个用于减少日期,所以你能告诉我如何管理它我用它来通过这种方式显示文本字段中的当前日期:

NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];

[dateformate setDateFormat:@"dd/MM/YYYY"];

date_String=[dateformate stringFromDate:[NSDate date]];
_dateTF.text = date_String;

2 个答案:

答案 0 :(得分:1)

  NSDate *decreaseDate= [NSDate dateWithTimeInterval:-60 * 60 * 24 sinceDate:<somedate>];
  NSDate *inceraseDate= [NSDate dateWithTimeInterval:60 * 60 * 24 sinceDate:<somedate>];

上面的代码段将帮助您获取上一个和下一个日期。

答案 1 :(得分:0)

您可以使用此代码

//This code will give you next date from current date

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1]; // you can change this as per your requirement, this will increase +1 date
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];


// This code will give previous date from current date

NSDate *today1 = [[NSDate alloc] init];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents1 = [[NSDateComponents alloc] init];
[offsetComponents1 setDay:-1]; // you can change this as per your requirement, this will decrease -1 date
NSDate *previousDate = [gregorian1 dateByAddingComponents:offsetComponents1 toDate:today1 options:0];

您可以根据需要更改日期格式date_format。希望这可以帮助。 快乐的编码。