How do I get the current date in Cocoa

后端 未结 14 449
醉酒成梦
醉酒成梦 2020-12-04 09:39

I\'m getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I\'m try

相关标签:
14条回答
  • 2020-12-04 10:15

    You need to do something along the lines of the following:

    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:now];
    NSLog(@"%d", [components hour]);
    

    And so on.

    0 讨论(0)
  • 2020-12-04 10:15
    + (NSString *)displayCurrentTimeWithAMPM 
    {
        NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
        [outputFormatter setDateFormat:@"h:mm aa"];
    
        NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]];
    
        return dateTime;
    }
    

    return 3:33 AM

    0 讨论(0)
  • 2020-12-04 10:17

    Replace this:

    NSDate* now = [NSDate date];
    int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
    int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
    int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec];
    

    With this:

    NSDate* now = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger second = [dateComponents second];
    [gregorian release];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second];
    
    0 讨论(0)
  • 2020-12-04 10:18

    Also, I've noticed at different places and at different times the asterisk (*) is located either right after the time NSDate* now or right before the variable NSDate *now. What is the difference in the two and why would you use one versus the other?

    The compiler doesn't care, but putting the asterisk before the space can be misleading. Here's my example:

    int* a, b;
    

    What is the type of b?

    If you guessed int *, you're wrong. It's just int.

    The other way makes this slightly clearer by keeping the * next to the variable it belongs to:

    int *a, b;
    

    Of course, there are two ways that are even clearer than that:

    int b, *a;
    
    int *a;
    int b;
    
    0 讨论(0)
  • 2020-12-04 10:26

    You must use the following:

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere];
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger second = [dateComponents second];
    [gregorian release];
    

    There is no difference between NSDate* now and NSDate *now, it's just a matter of preference. From the compiler perspective, nothing changes.

    0 讨论(0)
  • 2020-12-04 10:26

    NSDate* now and NSDate *now are the same thing: a pointer to an NSDate object.

    You probably want to use descriptionWithCalendarFormat:timeZone:locale: rather than dateWithCalendarFormat: — the latter returns an NSCalendarDate, which the docs say is scheduled to be deprecated at some point.

    0 讨论(0)
提交回复
热议问题