How do I get hour and minutes from NSDate?

后端 未结 8 1402
清歌不尽
清歌不尽 2020-12-02 05:26

In my application I need to get the hour and minute separately:

NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)];
        int currentHour         


        
8条回答
  •  忘掉有多难
    2020-12-02 06:09

    If you were to use the C library then this could be done:

    time_t t;
    struct tm * timeinfo;
    
    time (&t);
    timeinfo = localtime (&t);
    
    NSLog(@"Hour: %d Minutes: %d", timeinfo->tm_hour, timeinfo->tm_min);
    

    And using Swift:

    var t = time_t()
    time(&t)
    let x = localtime(&t)
    
    println("Hour: \(x.memory.tm_hour) Minutes: \(x.memory.tm_min)")
    

    For further guidance see: http://www.cplusplus.com/reference/ctime/localtime/

提交回复
热议问题