• 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
NSDateComponents question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSDateComponents question


  • Subject: NSDateComponents question
  • From: Jason Wiggins <email@hidden>
  • Date: Tue, 1 Jul 2008 02:45:45 +1000

Hi everyone,

Since NSCalendarDate is going to be deprecated in the future, I've started playing with NSDateComponents. I have a question regarding a problem I'm having. I'm getting a wacky date returned at line 15. I'm expecting it to add -3 to the current date, but I get something *way* in the future. I'm not trying to invent a time machine. Here is the code I have:

1 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
2 NSDate *now;
3 NSDate *startDate;
4 NSDate *endDate;
5 NSDateComponents *comps = [[NSDateComponents alloc] init];
6 NSDateComponents *components = [[NSDateComponents alloc] init];
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];
15 NSLog(@"startDate is: %@", startDate); // prints "startDate is: 4017-02-01 02:32:04 +1100" to the console


If I change it to the following code, it works as expected:

1 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
2 NSDate *now;
3 NSDate *startDate;
4 NSDate *endDate;
5 NSDateComponents *comps = [[NSDateComponents alloc] init];
6 NSDateComponents *components = [[NSDateComponents alloc] init];
7 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
8 int x = 0;
9 now = [NSDate date];
10 components = [calendar components:unitFlags fromDate:now];
11 x = [components 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];
15 NSLog(@"startDate is: %@", startDate); // prints "startDate is: 2008-06-28 02:36:48 +1000" to the console


What I don't understand is why just trying to add -3 in the first lot of code gives me a strange date. Why do I need to get two different NSDateComponents to do what I want to do, or am I missing something? I've read the docs: file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html but haven't found an answer.

Thanks,
JJ
_______________________________________________

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: mmalc crawford <email@hidden>
  • Prev by Date: [OT] Re: Style Question
  • Next by Date: Some advice re. "saving" a variable so it is accessible to all classes/methods
  • Previous by thread: [ANN] CocoaHeads Frankfurt
  • Next by thread: Re: NSDateComponents question
  • Index(es):
    • Date
    • Thread