• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDateComponents question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDateComponents question


  • Subject: Re: NSDateComponents question
  • From: Jason Wiggins <email@hidden>
  • Date: Tue, 1 Jul 2008 20:41:56 +1000

Thanks mmalc, much appreciated. I'll give it a go when I get time to code.

Regards,
JJ


On 01/07/2008, at 6:08 PM, mmalc crawford wrote:


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


References: 
 >Re: NSDateComponents question (From: Jason Wiggins <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)

  • Prev by Date: Re: Process app.
  • Next by Date: Font related question
  • Previous by thread: Re: NSDateComponents question
  • Next by thread: Re: NSDateComponents question
  • Index(es):
    • Date
    • Thread