单击下一个和上一个 UIButton 时更改 UILabel 中的日期

Change date in UILabel when click next and previous UIButton

UILabel 中设置一个日期。 单击 "next" 和 "previous" 按钮应该会更改此标签内的日期。

我正在尝试此代码,但单击下一个按钮并显示第二天的日期,例如 01/05/2015 和上一个按钮单击并设置日期 29/05/2015 和下一个按钮单击不显示 02/05/ 2015

- (IBAction)changeToNextDay:(id)sender
{
    NSDateComponents* deltaComps = [[NSDateComponents alloc] init];
    [deltaComps setDay:+1];
    NSDate* tomorrow = [[NSCalendar currentCalendar]  dateByAddingComponents:deltaComps toDate:[NSDate date] options:0];

    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *stringFromDate = [myDateFormatter  stringFromDate:tomorrow];
    dateLabel.text = stringFromDate;
}

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

    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *stringFromDate = [myDateFormatter stringFromDate:datePlusOneDay];
    dateLabel.text = stringFromDate;
}

前一天的函数

-(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 *previousDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:previousDay];

}

第二天函数

-(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 *nextDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:nextDay];;

}

你可以使用这个功能

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Update_Date_By:0];
}

- (IBAction)changeToNextDay:(id)sender {
    [self Update_Date_By:1];
}

- (IBAction)changeToPreviousDay:(id)sender {
    [self Update_Date_By:-1];
}

-(void)Update_Date_By:(NSInteger)value {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date = [dateFormat dateFromString:dateLabel.text];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [NSDateComponents new];
    dateComponents.day = value;
    if (value == 0) {
        date = [NSDate date];
    }
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString *finalDate_String = [dateFormat stringFromDate:newDate];
    dateLabel.text = finalDate_String;
}

您可以通过简单的 way.Follow 步骤实现此操作-

  1. 声明两个全局变量,用于格式化和递增或递减 date.Such as-

    #import "ViewController.h"

    @interface ViewController ()
    {

    NSDateFormatter *DateFormatter;
    int day;
    

    }

    @end
    
  2. 在viewDidLoad中,定义日期的格式,在UILabel中设置当前日期,并初始化day变量为0-

    day=0;
    DateFormatter=[[NSDateFormatter alloc] init];
    [DateFormatter setDateFormat:@"dd/MM/yyyy"];
    dateLabel.text=[DateFormatter stringFromDate:[NSDate date]];
    
  3. 为了去第二天把代码写在'changeToNextDay'按钮的body-

    NSDate *now = [NSDate date];
    day+=1;
    NSDateComponents *components =[[NSDateComponents alloc] init];
    [components setDay:day];
     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];  
     NSDate *newDate = [gregorian dateByAddingComponents:components toDate:now options:0];
     dateLabel.text=[DateFormatter stringFromDate:newDate];
    
  4. 要转到前一天,只需从 'changeToNextDay' 复制代码并将其粘贴到 'changeToPreviousDay' 按钮并更改行 -

     day-=1 from day+=1;
    

让我知道它是否适合 you.Thank 你。