Here i use Some label to display the date . And i have 2 arrow button to move back and previous dates.Like below:
(button) < **27Dec 28Dec 29Dec 30Dec 31Dec 1Jan** >(Button)
Here is the code for that:
- (void)viewDidLoad {
[super viewDidLoad];
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];
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=#"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 {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}
- (IBAction)calLeft:(id)sender {
firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
[self dateChange];
}
The above code will work like if i press back button it will go by one by one date change:
< **27Dec 28Dec 29Dec 30Dec 31Dec 1Jan** >
Now if i press left arrow:
< **26Dec 27Dec 28Dec 29Dec 30Dec 31Dec** >
But what i need if when i press left arrow or right arrow it should by week by week like this:
< **27Dec 28Dec 29Dec 30Dec 31Dec 1Jan** >
Now if i press left arrow:
< **21Dec 22Dec 23Dec 24Dec 25Dec 26Dec** >
Now if i press left arrow again its should like this:
< **16Dec 16Dec 17Dec 18Dec 19Dec 20Dec** >
Please what should i need to change to get like that to change date in week wise.Please help me!
If you want the buttons to change firstDate by 7 days instead of one, change your button methods to:
- (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];
}
Note: your current code using 86400 is bad. Never use such code to do date math.
Related
How to check the date cross the Sunday and reset the data?
I need to do reset on every Sunday, if I missed that on Sunday then the reset need to happen if user open the app the following increment date(i.e mon,tue, till saturday).
For Every Sunday or crossed the Sunday one time the reset need to happened, if user forgot to open the application on Rest date.
Following the my code which i try to implemented.
Which was not working as per my requirement, Your feedback is highly appreciated.
-(void) weekDaySundayToRefresh
{
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekday fromDate:now];
NSUInteger weekdayToday = [components weekday];
NSInteger daysToMonday = (8 - weekdayToday) % 7;
nextSunday= [now dateByAddingTimeInterval:60*60*24*daysToSunday];
NSLog(#"nextMonday : %#", nextSunday);
}
-(void) compareDate
{
NSDate *today = [NSDate date]; // it will give you current date
// NSDate *newDate = [MH_USERDEFAULTS objectForKey:#"USER_BACKGROUND_DATE"]; // your date
NSComparisonResult result;
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
result = [today compare:refreshGoalsDate]; // comparing two dates
if(result==NSOrderedAscending)
{
NSLog(#"today is less");
}
else if(result==NSOrderedDescending)
{
NSLog(#"newDate is less");
NSDate *fromDate;
NSDate *toDate;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate
interval:NULL forDate:refreshGoalsDate];
[calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate
interval:NULL forDate:today];
NSDateComponents *difference = [calendar components:NSCalendarUnitDay
fromDate:fromDate toDate:toDate options:0];
noOfDaysCount = [difference day];
NSLog(#"date difference:%ld",(long)[difference day]);
}
else
NSLog(#"Both dates are same");
}
-(void) checkRefreshGoals:(int)sectionIndex rowIndex:(int)rowIndex
{
NSDateFormatter * formatter = [NSDateFormatter new];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString * currentDateString = [formatter stringFromDate:[NSDate date]];
NSString * initialNotificationDate = [formatter stringFromDate:refreshDate];
NSDate *currentDate = [formatter dateFromString:currentDateString];
NSDate *refreshDate = [formatter dateFromString:initialNotificationDate];
if ([refreshDate compare:currentDate] == NSOrderedDescending)
{
NSLog(#"date1 is later than date2");
isGoalsRefreshed = NO;
}
else if ([refreshDate compare:currentDate] == NSOrderedAscending)
{
NSLog(#"date1 is earlier than date2");
if (sameDay == YES)
{
isGoalsRefreshed = YES;
[self refreshDataOnSunday:sectionIndex rowIndex:rowIndex];
}
else if (noOfDaysCount > 7)
{
isGoalsRefreshed = YES;
[self refreshDataOnSunday:sectionIndex rowIndex:rowIndex];
}
else
{
isGoalsRefreshed = NO;
}
}
else
{
NSLog(#"dates are the same");
isRefreshed = NO;
}
}
-(void) refreshDataOnSunday:(int)sectionIndex rowIndex:(int)rowIndex
{
if (noOfDaysCount > 7)
{
if (isGoalsRefreshed == YES)
{
// Do rest your data
}
}
else if (sameDay == YES)
{
if (isGoalsRefreshed == YES)
{
// Do reset your data
}
}
else
{
isGoalsRefreshed = NO;
}
}
First you need to save todays date and DayNumber when user opens your app at first time and then you need to compare next app open date so you can estimate either Sunday is gone or not.
/*first check in userDefault for stored Date and Day
Store values in local and check with current date */
NSMutableDictionary *dictStoredData = [USERDEFAULT valueForKey:#"dateTimeDate"];
if (!dictStoredData) {
// user comes first time so store it here
dictStoredData =[[NSMutableDictionary alloc] init];
NSDate *today =[NSDate date];
NSCalendar *gregorian =[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps =[gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger weekday = [comps weekday];
int nextSundayVal = 7 - (int)weekday;
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
NSString *startDate = [format stringFromDate: today];
[dictStoredData setObject:startDate forKey:#"startDate"];
[dictStoredData setObject:[NSString stringWithFormat:#"%d",nextSundayVal] forKey:#"nextSundayVal"];
[USERDEFAULT setObject:dictStoredData forKey:#"dateTimeDate"];
}
else{
NSString *strEndDate = #"2017-01-08";
NSString *strStartDate = [dictStoredData valueForKey:#"startDate"];
int nextSundayVal = (int)[dictStoredData valueForKey:#"nextSundayVal"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
NSDate *startDate = [format dateFromString:strStartDate];
NSDate *endDate = [format dateFromString:strEndDate];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
fromDate:startDate
toDate:endDate
options:0];
int newDayCount = (int)[components day];
if (newDayCount > nextSundayVal) {
NSLog(#"sunday is gone so reload data!!");
//save this date as a new date and check with this when user come again
}
else {
NSLog(#"sunday is not come yet!!");
}
}
here strEndDate is next date when user opens your application so you can get it from [NSDate date] with same dateformater.
I have run this code in my machine and its printing sunday is gone so reload data so hope this will help you.
As like you we had flow, every friday and more than seven days, its need to update the server.
We did it with following code:
You need to update last updated date.
Check this out:
if ([isFirday isEqualToString:#"Friday"]) {//This is my value from server side (they will give as friay), i think you dont need this condition.
BOOL isSameDay;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy,HH:mm"];
NSDate *date = [dateFormatter dateFromString:mennuRefreshTime];//Is an last updated date.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *temp = [dateFormatter stringFromDate:date];
NSDate *lastDate = [dateFormatter dateFromString:temp];
NSString *currentDaystr = [dateFormatter stringFromDate:[NSDate date]];
// Write the date back out using the same format
NSDate *currentDate = [dateFormatter dateFromString:currentDaystr];
if ([lastDate compare:currentDate] == NSOrderedDescending) {
NSLog(#"date1 is later than date2");
isSameDay = NO;
} else if ([lastDate compare:currentDate] == NSOrderedAscending) {
NSLog(#"date1 is earlier than date2");
[dateFormatter setDateFormat:#"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:currentDate];
NSString *lastupdateName = [dateFormatter stringFromDate:lastDate];
if ([dayName isEqualToString:lastupdateName]) {
isSameDay = YES;
}else{
isSameDay = NO;
}
} else {
NSLog(#"dates are the same");
isSameDay = YES;
}
if (isSameDay == YES) {
return;
}
NSCalendar *calender =[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calender components:NSCalendarUnitDay fromDate:lastDate toDate:currentDate options:0];
int day = (int)[components day];
if (day >= 7) {
[self getTopList];
}else{
[dateFormatter setDateFormat:#"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:currentDate];
if ([dayName isEqualToString:isFirday]) {
[self getTopList];
}else{
return;
}
}
}
I have some 6 UILabels which will show the date like below:
Jan,2016 (label)
(button) < **27Dec 28Dec 29Dec 30Dec 31Dec 1Jan** >(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:
- (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];
[self dateChange];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM,yyyy"]; dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
dateLabel.text = [dateFormat stringFromDate: firstdate];
}
-(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=#"MMM,yyyy";
dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
}
}
}
- (IBAction)calRight:(id)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}
- (IBAction)calLeft:(id)sender {
firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
[self dateChange];
}
So what I need in .For example like says - today is Jan 1 .so if user open my app they will seeJan 1 at last date followed by previous december date at back like I mention above .
So in that case if user on current datemeans like Jan1 means then my Rightbuttonshould be disabled. At the same time when user use left button to go november month menas then my right buttonshould be enabled
Only my rightbuttonshould be disabled when user in current date.
Please help me to solve I have an idea that !
if (some condition)
[leftBtn setEnabled:NO];
else
[leftBtn setEnabled:YES];
But I am not sure about condition and code.
Change your method dateChange with bellow code and also btnRight is your right side button so change name whatever you have give for it.
-(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 < labelArray.count; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==(labelArray.count-1)) {
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]]])
btnRight.enabled = false; //It's the same day
else
btnRight.enabled = true;
}
}
}
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];//You Can Change Date Format
NSString *strDate=[dateFormatter stringFromDate:[NSDate date]];
[btnYourButton setEnabled:([strDate isEqualToString:txtYourTextFieldDate.text])?NO:YES];
NSCalendar has a very convenient method to check if today contains a specific date.
rightButton.enabled = ![[NSCalendar currentCalendar] isDateInToday:aDate];
Important note:
Never ever do date math using the 86400 constant
Consider that on a day where daylight saving time changes a day doesn't have 86400 seconds.
For example instead of
firstdate = [NSDate date];
firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];
write
firstdate = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay value:-5 toDate:[NSDate date] options:nil];
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;
}
}
}
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];
}
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....
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.
I have get Sequence of month using following code
NSDate *aDate = [NSDate dateWithTimeInterval:row*30*24*60*60 sinceDate:self.earliestPresentedDate];
NSDateComponents *components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [self.calendar dateFromComponents:components];
components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:aDate];
NSDate *otherDate = [self.calendar dateFromComponents:components];
if ([today isEqualToDate:otherDate]) {
} else {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"MMM ";
[lblDate setText:[formatter stringFromDate:aDate]];
}
i want to get Sequence of year how to get...please give solution
Make one function which gives to date of next year as below,
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.year = +1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *newDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"yyyy";
NSString *strDate = [formatter stringFromDate:newDate];
NSLog(#"%#",strDate);
Set this date in your label.
above I am passing current date so I Will get year : 2016.
Simple way, it logs this and the subsequent 7 years.
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
NSLog(#"%ld", thisYear++);
}
or to get an array of strings
NSMutableArray *theYears = [NSMutableArray array];
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
[theYears addObject:[NSString stringWithFormat:#"%ld", thisYear++]];
}
NSLog(#"%#", theYears);
I cannot use a UIDatePicker. I just want to throw that out at the very beginning before all the commenters tell me to just use a UIDatePicker. I want to have a Picker Wheel that shows all the day of the year, leading up to today, and display it as Day: 1/Jan 1, Day: 2/Jan 2 and so on. Here is what I have so far, which detects what day 'number' of the year it is, and populates the PickerWheel with all the dates leading up to today:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSString *pickerItemTitle = [NSString stringWithFormat: #"Day: %d", index];
[options addObject: pickerItemTitle];
}
What can I do to make it also match up the NSInteger with the date, and have that be part of the pickerItemTitle as well?
Got it figured out. Added some code to form a date based off integer of day into the loop. Now, the viewWillAppear method that sets the array for the uipickerview looks like this:
- (void)viewWillAppear:(BOOL)animated
{
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:index];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMM dd"];
NSString *articleDateString = [formatter stringFromDate:date];
NSString *pickerItemTitle = [NSString stringWithFormat: #"Day: %d/%#", index, articleDateString];
[options addObject: pickerItemTitle];
}
[super viewWillAppear:YES];
}