I am able to display the calendar (dates) of this month in table view and I am highlighting the current date and it is working fine, but now what I want to do is to display next coming 2 weeks from the current date.
I am using the following code to get the dates of the month
NSDate *today = [NSDate date];
NSCalendar *calender = [NSCalendar currentCalendar];
days = [calender rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
How to calculate the up coming two weeks from today?
Check my answer from this post. There you can set value for daysToAdd that you want and get the NSDate corresponding.
May be you can try with:
NSDate *inTwoWeeks = [NSDate dateWithTimeIntervalSinceNow:60*60*24*7*2]
Related
In my app i am showing, the list of months in table view. For each month there are few events in it for some dates. So my question is how to compare the dates in my array with the dates of month to show the events in their respective months only.
you can use NSDateComponents for that:
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components =
[calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:yourDate];
[components month];// gives you month
Compare this month with the Array index for example or another way you prefer.
In your case probably you only need to compare the year and month component of the date.
There's probably many ways for implementing this.
This is what i would have tried. Convert your date in tableview in yyyy-MM Format and also the event dates in the same format probably the way given below.
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM"];
dateFormatter1.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSDate *monthDate=[NSDate date];
NSString *strMonthDate=[dateFormatter1 stringFromDate:monthDate];
NSLog(#"%#",strMonthDate);
Compare this monthDate string with your eventDate string using normal NSString comparison methods.
So i try to get this weeks date range.
When i get current date components (only year and date) these are the values:
I noticed that there is "Obsolescent" written near week. Did try searching apple docs but found nothing about this.
Then i try to get the NSDate with current calendar (or gregorian calendar, same value) but the value i get is:
So weeks got ignored. So i added few months and one week to my components and had such components:
Then i tried to get my date out of this, this is what i got:
Now clearly week components are getting ignored here. Why is that so? Is it deprecated? Or is it some bug?
So i found the solution finally.
It appears that it all sorts out (starts working) if you just set weekday:
[startDate setWeekday:1];
Then the date with week is returned correctly.
Do not know if it is still a bug when week components get ignored without setting weekday.
To compute the start end date of the a week, the rangeOfUnit:startDate:interval:forDate: method of NSCalendar is quite useful:
NSDate *now = [NSDate date];
NSDate *startOfWeek;
NSTimeInterval length;
[[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfWeek
interval:&length
forDate:now];
NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length];
NSLog(#"%#", startOfWeek);
NSLog(#"%#", endOfWeek);
To compute the same date a week before, use dateByAddingComponents:
NSDate *now = [NSDate date];
NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setWeek:-1]; // One week back in time
NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:now options:0];
NSLog(#"%#", now);
NSLog(#"%#", date);
Even though i do not have an answer to my question i will post solution to this issue that i used.
So i took weeks and multiplied them with 7. This gave me current week end date. I know it is not good since weeks can have different days set, etc. But for my most default issue (gregorian calender, 7 days a week) this solution was fast workaround in already existing date time calculator.
[components setDay:components.week * 7 - 7]; //Start of week
startDate = [[NSCalendar currentCalendar] dateFromComponents:components];
[components setDay:components.day + 7]; //End of week
endDate = [[NSCalendar currentCalendar] dateFromComponents:components];
[components setDay:0]; //I am using this not just here so i am setting back my changes
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to compare two NSDate objects in objective C
1---> I'm new to Objective-C and stuck at a point. The concept is of type booking slots/reservation, which means cannot book a slot at previous time and previous date. I have to compare today's date with a particular date may be less than today's date or greater than today's date.
For getting current date I have done this :
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
I have a date which is to be compared with todays date in UItextField which is also in yyyy-MM-dd format.
Now I have to compare today's date with the textField date.But I'm not getting it ..
if(textfielddate<currentdate)
{
NSLog(#"You cannot book a slot at past date);
}
but here it is not getting compared..
2---> I need to compare a time in 24hr format with currenttime which is also in 24hr format..
I have a particular time in a string variable 12:00,11:00,15:00 etc .Now I need to compare this with the current time in 24 hr format .
if([time <= currenttime] && [textFielddate==currentdate])
{
NSLog(#"You cannot book a slot at past time ");
}
How can I do it?
BEst way is to use NSCompariosnResult
[date1 compare:date2];
then check whether ascending or descending or equal.
Regards
Deepak
For Checking whether the date entered in the UITextField is previous or not.
You can create a new date using the values entered in the textfield. Suppose the value entered is 06-Nov-2012 then from that textfield you have to get the day, month and year components in integers programmatically.
Get the hours, minutes and seconds also in integers for the time for which you need to check that it is a previous time or not. Create a date with desired time using:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:iDay];
[comps setMonth:iMonth];
[comps setYear:iYear];
[comps setHour:iHour];
[comps setMinute:iMinutes];
[comps setSecond:iSeconds];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateToCheck = [gregorian dateFromComponents:comps];
[comps release];
Once date is created you can check whether the date is previous one or not using :
double timestamp = [dateToCheck timeIntervalSinceDate:[NSDate date]];
if(timestamp < 0)
{
// The date is previous one
}
How do I get date from the day of the year?
for e.g., if I give day as "1" then date should be "January 1"
How do I get that in iPhone? I have found answer in javascript but I want to know how to do the achieve same thing in Objective?
I guess this code will work for you:
I have created a sample function where a textfield gives input values for how many days to add. And a button to calculate final day. Here is the button event logic.
NSDateComponents *dateComponent = [[NSDateComponents alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-mm-dd"];
NSDate *newDate = [formatter dateFromString:#"2012-01-01"];
// add days to current date
dateComponent.day = [dayTextField.text intValue];
// get a new date by adding components
newDate = [[NSCalendar currentCalendar] dateByAddingComponents: dateComponent toDate:newDate options:0];
[finalDate setText:[NSString stringWithFormat:#"%#", newDate]];
Here dayTextField.text is the text which says how many number of days want to calculated. (For example 125 days) and finalDate is an label which displays final generated date (means date after 125 days since 1 Jan 2012).
Benefit of this code is, any time you can change the start day parameter. For example, for other requirement, i need to count my days from "31 May 1999" then i will change it easily in one line and the same code will work.
Enjoy Coding :)
NSCalendar *gregorian =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger dayOfYear =
[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit forDate:[NSDate date]];
[gregorian release];
return dayOfYear;
Take from the post:
How do you calculate the day of the year for a specific date in Objective-C?
I want to find the current date and the date that 10 days before from current date.I know how to find the current date.please anybody help me to find the date which 10 days before from current date..
Thanks in advance.
You can use NSDateComponents to subtract the days from the current date.
NSDate *today = [NSDate date];
NSDateComponents *sub_date = [[NSDateComponents alloc] init];
[sub_date setDay:-10];
NSDate *tenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:sub_date
toDate:today
options:0];
[sub_date release];
NSLog(#"Ten Days Ago: %#", tenDaysAgo);
You can use dateWithTimeInterval:sinceDate: to subtract 10 days from the current date.
Code example:
NSDate *todayDate = [NSDate date];
NSDate *tenDaysAgoDate = [NSDate dateWithTimeInterval:-864000 sinceDate:todayDate];
The 864000 represents the seconds in ten days, the negative sets the calculation to days AGO, instead of forward.