2014-10-06 101 views
0

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

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

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

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

检查下面的帖子,可以解决您的问题 – 2014-10-06 07:31:10

回答

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]; 

您可以更改日期,甲酸,与你的愿望日期格式。希望这可以帮助。 快乐编码。

+0

感谢它的工作 – RaviJSS 2014-10-06 10:02:53

+0

你能接受这个答案吗?如果它工作。 @RaviJSS – Dhruvik 2014-10-06 12:28:58

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

上面的代码片段将帮助您得到一个和下一个日期。

相关问题