Re: NSDateComponents question
Re: NSDateComponents question
- Subject: Re: NSDateComponents question
- From: Jason Wiggins <email@hidden>
- Date: Tue, 1 Jul 2008 03:27:27 +1000
- Resent-date: Tue, 1 Jul 2008 17:40:05 +1000
- Resent-from: Jason Wiggins <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Cocoa Dev <email@hidden>
Thanks mmalc for your reply. What you say makes sense. So comps is the
whole date (now) minus 3 days. I wasn't expecting that.
I should've made it clear what I was trying to achieve. I want to set
the start date to the start of the week, hence line 11. 3 was just an
arbitrary figure that happens to be today (localised in Sydney at
3:20am)
I want to get the weekday and subtract *that* from the current date. I
can't get a weekday figure without line 7. Any suggestions?
JJ
On 01/07/2008, at 3:02 AM, mmalc crawford wrote:
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