• 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: 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


  • Follow-Ups:
    • Re: NSDateComponents question
      • From: Christopher Kane <email@hidden>
    • Re: NSDateComponents question
      • From: Jason Wiggins <email@hidden>
References: 
 >Re: NSDateComponents question (From: Jason Wiggins <email@hidden>)

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