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.
Related
This question already has answers here:
Current Week Start and End Date
(18 answers)
Closed 6 years ago.
please have a look to following lines of code:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:date] week];
now how we can get the first and last day of the given week number (assume that the week starts from monday to sunday).
also if it possible to get the array of all the dates belongs to the same week number.
To get the first day of the week the following code can be used.
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:now] week];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeek:weekNumber]; //Week number.
[comp setWeekday:1]; //First day of the week. Change it to 7 to get the last date of the week
NSDate *resultDate = [gregorian dateFromComponents:comp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, yyyy"];
NSString *myDateString = [formatter stringFromDate:resultDate];
NSLog(#"%#",myDateString);
as commented in the above code change the parameter of setWeekday method to 7 to get the last day of the week.
Once u have the first day and last day of the week u can get all the dates as mentioned in the below link.
Array for dates between two dates
Note: The week starts from Monday for the region setting UK.
To change the iPhone Calendar to show Monday as the first day of the week.
1. tap "Settings"
2. tap "General"
3. tap "International"
4. tap "Region Format"
5. select "united kingdom"
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
I want to calculate the year from current NSDate. Suppose my current date 14-06-2012 so, what would be last date after end of year. (In simple way I can explain you that, if I have date 01-01-2012 then after end of year I would get 31-12-2012). So, how do I calculate year form current date.
Following output I expect for my project:
Next or End year Date where year end (Like 31-12-2012).
Next or end year month (Last date month).
Next or end year (Last date year or next year).
It Should be NSDate format. Hope you would able to understand my point.
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:(NSYearCalendarUnit) fromDate:today];
[comps setDay:31]; // last day of year is always the same
[comps setMonth:12];
NSInteger year = [comps year]; // the current year
NSDate *lastDayOfYear = [gregorian dateFromComponents:comps]; // the last day of the year
You can use this:
NSDate *now = [NSDate date];
NSDateComponents *dc = [[NSDateComponents alloc] init];
[dc setYear:1];
NSDate *targetDateObject = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:now options:0];
[dc release];
Using NSDateComponents I know how to get the day component, but this gives me a value from 1 - 365, right? I need to get the days 1-30 of a specific month, how can I?
For example, I have an NSDate which might be 16th May. I want to be able to get the day, so it returns 16. Being the 16th day of that month (not the 16th day of the year).
Thanks.
EDIT:
I have this:
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:NSDayCalendarUnit fromDate:[[NSUserDefaults standardUserDefaults] objectForKey:#"dateSaved"]];
However comps.day returns 1 when it should equal what the date is saved as, say 4.
The day value of NSDateComponents gives you the day of month according to the given calendar:
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSDayCalendarUnit fromDate:date];
NSLog(#"%# / %d", date, comps.day);
gives
2012-02-04 15:27:36.682 testapp[1533:f803] 2012-02-04 14:27:36 +0000 / 4
although February, 4th would be the 35th day of the year.
I am slowly getting into iOS development and trying to create a count up timer from a specific date. I have figured out the code which gives me the interval in seconds but I could not figure out how to extract Year/Month/Day/Hour/Minute/Second values from it in order to display each value in its own label as a ticker.
So far I have figured out that the following will give me the interval between the 2 dates in seconds, what I am trying to do is to parse this and display on my view this as a ticker by updating the UILabel every second using NSTimer and calling selector once every 1 seconds and get something like this on my view:
6Years 10Months 13Days 18Hours 25Minutes 18Seconds (obviously each label will be updated accordingly as the time goes up)
NSDate *startDate = [df dateFromString:#"2005-01-01"];
NSTimeInterval passed = [[NSDate date] timeIntervalSinceDate: startDate];
Thanks
Use NSCalendar with the two dates:
- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts
Returns, as an NSDateComponents object using specified components, the difference between two supplied dates.
Example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSDate *startingDate = [dateFormatter dateFromString:#"2005-01-01"];
NSDate *endingDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger days = [dateComponents day];
NSInteger months = [dateComponents month];
NSInteger years = [dateComponents year];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSLog(#"%dYears %dMonths %dDays %dHours %dMinutes %dSeconds", days, months, years, hours, minutes, seconds);
NSLog output:
13Years 10Months 6Days 8Hours 6Minutes 7Seconds
Well, as "month" and "year" will of course depend on which month and which year it is (365 or 364 days in a year? 30 or 31 days in a month?), you cannot just take the time interval and extract years and months - you always have to get a full date, e.g. by adding your (modified) time interval back to the start date like
NSDate* newDate = [startDate dateByAddingTimeInterval:modifiedPassedTimeInterval];
From that date, you can easily extratc the year, month, and so on with NSDateComponents:
NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:newDate];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];