Reputation: 71
Set a date in UILabel
.
The click on "next" and "previous" buttons should change date inside this label.
I'm trying this code but next button click and show date next day date for e.g 01/05/2015 and previous button click and set date 29/05/2015 and next button click not display 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;
}
Upvotes: 1
Views: 1069
Reputation: 643
- (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;
}
Upvotes: 6
Reputation: 1318
You can implement this in a simple way.Follow the steps-
Declare two global variables for formatting and incrementing or decrementing the date.Such as-
#import "ViewController.h"
@interface ViewController ()
{
NSDateFormatter *DateFormatter;
int day;
}
@end
In viewDidLoad,define the format of the date,set the current date in UILabel and initialize the day variable to 0-
day=0;
DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"dd/MM/yyyy"];
dateLabel.text=[DateFormatter stringFromDate:[NSDate date]];
For going to the next day Write the code in the body of the 'changeToNextDay' button-
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];
For going to the previous day just copy the code from 'changeToNextDay' and paste it in 'changeToPreviousDay' button and change the line-
day-=1 from day+=1;
Let me know if it works for you.Thank you.
Upvotes: 1
Reputation: 1327
Function for Previous Day
-(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];
}
function for next day
-(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];;
}
you can use this function
Upvotes: 0