date not showing in correct manner

2019-09-21 20:55发布

I have some 7 UILabels which will show the date like below:

                    Jan,2016 (label)
   (button)  < **29Dec 30Dec 31Dec 1Jan 2Jan 3Jan 4Jan** >(Button)

Also I am having two arrow UIButton at both side when user can move to back date or previous date. And i have one lable to show the current month based on my date showing

Here is my full code:

Viewcontroller.h

@property (weak, nonatomic) IBOutlet UILabel *flabel;
@property (weak, nonatomic) IBOutlet UILabel *slabel;

@property (weak, nonatomic) IBOutlet UILabel *tlabel;

@property (weak, nonatomic) IBOutlet UILabel *folabel;

@property (weak, nonatomic) IBOutlet UILabel *fivlabel;
@property (weak, nonatomic) IBOutlet UILabel *sixlabel;
@property (weak, nonatomic) IBOutlet UILabel *sevenlabel;

Viewcontroller.m

#import "ViewController.h"

@interface ViewController (){

    NSDate *firstdate;
}

@end

@implementation ViewController
@synthesize flabel;
@synthesize slabel;
@synthesize tlabel;
@synthesize folabel;
@synthesize fivlabel;
@synthesize sixlabel;
@synthesize sevenlabel;
@synthesize dateLabel;
@synthesize walletView;
@synthesize leftBtn;
@synthesize rightBtn;



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    firstdate = [NSDate date];
//    firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];

    firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-5 toDate:[NSDate date] options:nil];
//    




    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMMM,yyyy"];
    dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
    dateLabel.text = [dateFormat stringFromDate: firstdate];

    [self dateChange];
}


-(void)dateChange
{
    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";
    for (NSInteger i = 0; i < 7; ++i) {
        NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];
        if (i==6) {
            dateFormatter.dateFormat=@"MMM,yyyy";
            dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];

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

            if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
            {
                leftBtn.enabled = false;
                //It's the same day
            }
            else
            {
                leftBtn.enabled = true;
            }
        }
    }
}
- (IBAction)calRight:(id)sender {

    NSCalendar *calendar = [NSCalendar currentCalendar];
    firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:7 toDate:firstdate options:nil];
    [self dateChange];
    //////


}

- (IBAction)calLeft:(id)sender {

    NSCalendar *calendar = [NSCalendar currentCalendar];
    firstdate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-7 toDate:firstdate options:nil];
    [self dateChange];


}

Its already worked for 6 label but when i add 7 label i am not getting correct date. And also in this code my left button will be disabled when the las lable had todays date.

But i am getting result like this:

                    Jan,2016 (label)
   (button)  < **30Dec 31Dec 1Jan 2Jan 3Jan 4Jan 17Dec** >(Button)

1条回答
地球回转人心会变
2楼-- · 2019-09-21 21:20

Try to change like this:

In ViewDidload:

firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-6 toDate:[NSDate date] options:nil];

// And below method

-(void)dateChange
{
    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel,sevenlabel];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";
    for (NSInteger i = 0; i < 7; ++i) {
        NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];
        if (i==6) {
            dateFormatter.dateFormat=@"MMM,yyyy";
            dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];

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

            if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
            {
                leftBtn.enabled = false;
                //It's the same day
            }
            else
            {
                leftBtn.enabled = true;
            }
        }
    }
}

Try it . And let me know if its works

查看更多
登录 后发表回答