How do I get hour and minutes from NSDate?

后端 未结 8 1369
清歌不尽
清歌不尽 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:05

    If you only need it for presenting as a string the following code is much easier

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    
    NSString *startTimeString = [formatter stringFromDate:startTimePicker.date];
    

提交回复
热议问题