How do I get the current date in Cocoa

后端 未结 14 447
醉酒成梦
醉酒成梦 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:08
    CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem());
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%2.0f", currentDate.hour, currentDate.minute, currentDate.second];
    

    Mark was right this code is MUCH more efficient to manage dates hours min and secs. But he forgot the @ at the beginning of format string declaration.

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

    You can also use:

    
    CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem());
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02.0f", currentDate.hour, currentDate.minute, currentDate.second];
    CFRelease(currentDate); // Don't forget this! VERY important
    

    I think this has the following advantages:

    1. No direct memory allocation.
    2. Seconds is a double instead of an integer.
    3. No message calls.
    4. Faster.
    0 讨论(0)
  • 2020-12-04 10:09

    It took me a while to locate why the sample application works but mine don't.

    The library (Foundation.Framework) that the author refer to is the system library (from OS) where the iphone sdk (I am using 3.0) is not support any more.

    Therefore the sample application (from about.com, http://www.appsamuck.com/day1.html) works but ours don't.

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

    // you can get current date/time with the following code:

        NSDate* date = [NSDate date];
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
        formatter.timeZone = destinationTimeZone;
        [formatter setDateStyle:NSDateFormatterLongStyle];
        [formatter setDateFormat:@"MM/dd/yyyy hh:mma"];
        NSString* dateString = [formatter stringFromDate:date];
    
    0 讨论(0)
  • 2020-12-04 10:13

    Original poster - the way you're determining seconds until midnight won't work on a day when daylight savings starts or ends. Here's a chunk of code which shows how to do it... It'll be in number of seconds (an NSTimeInterval); you can do the division/modulus/etc to get down to whatever you need.

    NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]];
    [dc setDay:dc.day + 1];
    NSDate *midnightDate = [[NSCalendar currentCalendar] dateFromComponents:dc];
    NSLog(@"Now: %@, Tonight Midnight: %@, Hours until midnight: %.1f", [NSDate date], midnightDate, [midnightDate timeIntervalSinceDate:[NSDate date]] / 60.0 / 60.0);
    
    0 讨论(0)
  • 2020-12-04 10:14

    There is no difference in the location of the asterisk (at in C, which Obj-C is based on, it doesn't matter). It is purely preference (style).

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