Re: NSDateComponents question
Re: NSDateComponents question
- Subject: Re: NSDateComponents question
- From: mmalc crawford <email@hidden>
- Date: Mon, 30 Jun 2008 10:02:31 -0700
On Jun 30, 2008, at 9:45 AM, Jason Wiggins wrote:
5 NSDateComponents *comps = [[NSDateComponents alloc] init];
6 NSDateComponents *components = [[NSDateComponents alloc] init];
Why are you creating these objects -- you're just leaking them?
7 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSWeekdayCalendarUnit;
8 int x = 0;
9 now = [NSDate date];
10 comps = [calendar components:unitFlags fromDate:now];
11 x = [comps weekday];
12 NSLog(@"Weekday is: %i", x); // prints "Weekday is: 3" to the console
13 [comps setDay:-x];
14 startDate = [calendar dateByAddingComponents:comps toDate:now
options:0];
What do you get if you print the other components of comps to the
console?
Since you initialised 'comps' from 'now', the year component will be
2008. So add another 2008 years to 2008 and you'll get 4016 -- add
another six months doubled and you'll get 4017...
If you simply want to subtract 3 days from the current date, create a
new date components with a day component of -3.
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:-3];
NSDate *date = [gregorian dateByAddingComponents:comps
toDate:currentDate options:0];
[comps release];
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden