Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

ios - date not showing in correct manner

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...