How do I get the current date in Cocoa

后端 未结 14 448
醉酒成梦
醉酒成梦 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:30

    You have problems with iOS 4.2? Use this Code:

    NSDate *currDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    NSLog(@"%@",dateString);
    

    -->20.01.2011 10:36:02

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

    Here's another way:

    NSDate *now = [NSDate date];
    
    //maybe not 100% approved, but it works in English.  You could localize if necessary
    NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"midnight tomorrow"]; 
    
    //num of seconds between mid and now
    NSTimeInterval timeInt = [midnight timeIntervalSinceDate:now];
    int hours = (int) timeInt/3600;
    int minutes = ((int) timeInt % 3600) / 60;
    int seconds = (int) timeInt % 60;
    

    You lose subsecond precision with the cast of the NSTimeInterval to an int, but that shouldn't matter.

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