Re: NSDateComponents question
Re: NSDateComponents question
- Subject: Re: NSDateComponents question
- From: mmalc crawford <email@hidden>
- Date: Tue, 01 Jul 2008 01:08:38 -0700
On Jun 30, 2008, at 10:27 AM, Jason Wiggins wrote:
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?
Assuming you want just the actual day of the beginning of the week,
rather than a specific time on the day (and assuming a Gregorian
calendar and that you want the week to start on a Sunday):
NSDate *currentDate = [NSDate date];
/*
Get the components required to:
(a) determine the current day of the week, and
(b) create the day of the beginning of the week.
*/
NSDateComponents *components = [gregorian
components:NSWeekdayCalendarUnit | NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit fromDate:currentDate];
/*
Update the components to represent the beginning of the week by
subtracting the weekday number from the current day.
Weekday for Sunday in the Gregorian calendar is 1, so subtract 1 from
the number of days you want to subtract from the date in question.
(If today's Sunday, subtract 0 days.)
*/
[components setDay:([components day] - ([components weekday] -
1))];
// Create the day for the beginning of the week from the updated
components.
NSDate *beginningOfWeek = [gregorian
dateFromComponents:components];
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