How do I get the current date in Cocoa

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

提交回复
热议问题