In Objective-C, to get the current hour and minute as integers, we need to use NSDateComponents and NSCalendar?

11,110

Solution 1

NSDate just holds the time that has passed since a certain reference date, to get more meaningful numbers out of this (eg. after taking care of DST, leap years and all the other stupid time stuff), you have to use NSDateComponents with the appropriate NSCalendar.

Solution 2

How you interpret the seconds since 1970 depends on the calendar that you are using. There is simply no other option. Fortunately it is not that difficult to set up. See the 'Data and Time Programming Guide' for lots of examples. In your case:

// Assume you have a 'date'
NSCalendar *gregorianCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [gregorianCal components: (NSHourCalendarUnit | NSMinuteCalendarUnit)
                                              fromDate: date];
// Then use it
[dateComps minute];
[dateComps hour];

So it really isn't that complicated.

Also note that you could create a 'Class Category' to encapsulate this as:

 @interface NSDate (MyGregorianDateComponents)
  - (NSInteger) getGregorianHour;
  - (NSInteger) getGregorianMinute;
  @end

Solution 3

My class can help. https://github.com/TjeerdVurig/Vurig-Calendar/blob/master/Vurig%20Calendar/NSDate%2Bconvenience.m

I'm sure you can figure out the minute part :)

Share:
11,110
nonopolarity
Author by

nonopolarity

I started with Apple Basic and 6502 machine code and Assembly, then went onto Fortran, Pascal, C, Lisp (Scheme), microcode, Perl, Java, JavaScript, Python, Ruby, PHP, and Objective-C. Originally, I was going to go with an Atari... but it was a big expense for my family... and after months of me nagging, my dad agreed to buy an Apple ][. At that time, the Pineapple was also available. The few months in childhood seem to last forever. A few months nowadays seem to pass like days. Those days, a computer had 16kb or 48kb of RAM. Today, the computer has 16GB. So it is in fact a million times. If you know what D5 AA 96 means, we belong to the same era.

Updated on July 31, 2022

Comments

  • nonopolarity
    nonopolarity almost 2 years

    I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16.

    But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time representation, but there is no easy way to get them as integers?

    I see a post in Getting current time, but it involved NSDateComponents and NSCalendar? That's way too complicated... all that was need is something like

    NSDate *date = [NSDate date];
    int hour = [date getHour];     // which is not possible
    

    Is there a simpler way than using 3 classes NSDate, NSDateComponents, and NSCalendar to get the current hour as an integer, or typically, in Objective-C, would we typically still use C language's localtime and tm to get the hour as an integer?

    • nhahtdh
      nhahtdh almost 12 years
      I checked the reference, and I think there is no other way around this. NSDate seems to be just the number of seconds.
    • Omar Abdelhafith
      Omar Abdelhafith almost 12 years
      You could add a catalog on NSDate and extend it to calculate the getHour by using some seconds to date calculations, are you willing to do that?
  • Adam
    Adam almost 12 years
    Read the question again. There is a link.
  • Ken Thomases
    Ken Thomases almost 12 years
    To put it another way, an NSDate references a moment in time. That moment may be known as 3:16am where you are, but the same moment may be known as 6:16pm or even 12:46am someplace else. There is nothing about the moment alone which corresponds to "3:16am", you have to specify a time zone and a calendar, too.
  • nonopolarity
    nonopolarity almost 12 years
    that's true... or if it can be NSDateComponents dateComponents = [NSDateComponents forLocalTime]
  • nonopolarity
    nonopolarity almost 12 years
    At first I thought Calendar is like a monthly calendar / appointment kind of class, but turns out it is for "calendar for different cultures" (11 different calendar system as of right now)... so it is quite profound and useful... maybe there can be a convenience method for the most often used calendar system for NSDateComponents, but I suppose once you are familiar all 3 classes, they are quite powerful
  • JustSid
    JustSid almost 12 years
    @user523234 Yup, KenThomases explanation is way better! A shame that he won't get any reputation for the comment.
  • nonopolarity
    nonopolarity almost 12 years
    by the way, later on it seems that NSDateComponents wanted the bit flags so that it can give the required data by a one-time calculation, instead of calculate everything once for the hour, and then calculate all over again for the minute
  • Tjeerd in 't Veen
    Tjeerd in 't Veen almost 12 years
    Yes I agree that would be better for this situation