Reputation: 1699
here I used one label at top to display the today month with year. And also I have 6 label which will display the todays date like say 31dec to 5 Jan. Here is the code:
This will display the todays date from next 5 date
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
}
This will display the current month with year
// show the present month
NSDate *date = [NSDate date];
// NSDateFormatter *date = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
dateFormatter.dateFormat=@"yyyy";
NSString * yearString = [[dateFormatter stringFromDate:date] capitalizedString];
dateLabel.text = [NSString stringWithFormat:@"%@ ,%@",monthString,yearString];
Important:
what I need is I need to display like below image:
The current date should display at last and should show previous 5 dates before.
I have two button at left and right. when ever user press right button like say now its 31Dec. And when user press right it should change to 1Jan and should display the previous 5 date from Jan 1. And also the month .label should automatically change to Jan,2016. Like below image:
Two button action:
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
This code I did for changing only month I tried. But no changing my date label.
Please explain me in code.
Full Edited:
- (void)viewDidLoad {
[super viewDidLoad];
NSDate *firstLabelDate = [NSDate date]; //make it class variable
NSDate *lastLabelDate = [NSDate date]; //make it class variable
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate];
}
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?1:-1;
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
UILabel *label = (UILabel *)self.labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
Upvotes: 2
Views: 106
Reputation: 11276
Pretty simple actually you are almost there, add one more variable that will hold the last date displayed in the last label, let us not mess up by taking the date out of the label via NSDateFormatter etc. long route!
NSDate *firstLabelDate = [NSDate date]; //make it class variable
NSDate *lastLabelDate = [NSDate date]; //make it class variable
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
And your button actions should be modified as below,
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate]
}
Add a common method to reload your date labels.
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?(1*i):(-1*i);
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
UILabel *label = (UILabel *)self.labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
Hope this helps, and this could help you in the longer run.
Upvotes: 1
Reputation: 1739
Define one Date object globally NSDate *firstdate;
and assign its value in viewDidLoad firstdate = [NSDate date];
now use following method to show your dates & month call this method from viewDidLoad [self dateChange];
-(void)dateChange
{
NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==5) {
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateFormatter.dateFormat=@"yyyy";
NSString * yearString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateLabel.text = [NSString stringWithFormat:@"%@ ,%@",monthString,yearString];
}
}
}
now set your calRight and calLeft as bellow
- (IBAction)calRight:(id)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}
- (IBAction)calLeft:(id)sender {
firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
[self dateChange];
}
Upvotes: 1
Reputation: 438
First create an array which will store date's for 6 days...NSArray *dates
.
Then initialise it with [NSDate date]
at dates[0]
.
Use NSDateComponents
and subtract the date component and store the previous 5 days in the array from dates[1.....4]
.
On click of the button you can increment or decrement the entire array and display the dates all again....
Upvotes: 1