[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSCalendar Issue

From: Abhi Beckert
Subject: NSCalendar Issue
Date: Tue, 22 Jan 2013 15:59:10 +1000

I'm having a problem with NSCalendar. This is the code I'm using:

      NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

      NSDate *midnightThisMorning = [NSDate date];
      NSLog(@"%@", midnightThisMorning);

      NSDateComponents *comps = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit) fromDate:midnightThisMorning];
      midnightThisMorning = [gregorian dateFromComponents:comps];
      NSLog(@"%@", midnightThisMorning);

It works on Mac OS X and iOS. I get:

    2013-01-22 07:55:08 +1000
    2013-01-22 00:00:00 +1000

Bun on GNUstep I get this:

    2013-01-22 07:55:08 +1000
    1970-01-22 00:00:00 +1000

For some reason the year (and possibly month?) doesn't get copied across to the new date.

Am I doing something wrong? Or is NSCalendar buggy? All I really need is the UNIX timestamp for the beginning/end of the current day.

- Abhi

reply via email to

[Prev in Thread] Current Thread [Next in Thread]