日期没有以正确的方式显示

时间:2016-01-04 03:59:54

标签: ios objective-c nsdate nsdateformatter

我有一些UILabels,它会显示如下所示的日期:

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

当用户可以移动到返回日期或上一个日期时,我在两侧都有两个箭头UIButton。我有一个标签,根据我的日期显示当前月份

这是我的完整代码:

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


}

它已经为6个标签工作但是当我添加7个标签时我没有得到正确的日期。而且在这段代码中,当las lable有今天的日期时,我的左按钮将被禁用。

但我得到的结果是这样的:

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

1 个答案:

答案 0 :(得分:0)

尝试改变如下:

在ViewDidload中:

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

//以及方法

-(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;
            }
        }
    }
}

试试吧。如果它有效,请告诉我