• 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 17:41:34 -0700


On Jul 1, 2008, at 5:20 PM, Jason Wiggins wrote:

It look like what I thought was going to be something simple, has turned out to be less so and fraught with danger programatically.
I was just thinking though, would it be easier just to covert the weekday value into seconds (weekday * 60 * 60 * 24) and subtract that from the current date? This would save you from the issue of year and month boundaries. Following code is untested (written in Mail):



I'm sorry if I've obfuscated Chris's reply. I'd thought it would be possible to save a few steps by reusing the date components object you get from decomposing the start date, but Chris indicates that the short-cut is not guaranteed to work in all situations. His robust solution is to do the following:


NSDate *today = [NSDate date];
[NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];


// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
/*
Create a date components to represent the number of days to subtract from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number
of days to subtract from the date in question. (If today's Sunday, subtract 0 days.)
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: - ([weekdayComponents weekday] - 1)];


NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];

/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the original date (today).
To normalize to midnight, extract the year, month, and day components and create a new date from those components.
*/
NSDateComponents *components = [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: beginningOfWeek];
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: Jason Wiggins <email@hidden>
    • Re: NSDateComponents question
      • From: Jason Wiggins <email@hidden>
References: 
 >Re: NSDateComponents question (From: Jason Wiggins <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)
 >Re: NSDateComponents question (From: Christopher Kane <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)
 >Re: NSDateComponents question (From: Chris Kane <email@hidden>)
 >Re: NSDateComponents question (From: mmalc crawford <email@hidden>)
 >Re: NSDateComponents question (From: Jason Wiggins <email@hidden>)

  • Prev by Date: Re: NSDateComponents question
  • Next by Date: Re: KVO Bug With Keypaths
  • Previous by thread: Re: NSDateComponents question
  • Next by thread: Re: NSDateComponents question
  • Index(es):
    • Date
    • Thread