Getting Current Time in string in Custom format in objective c

后端 未结 4 1553
无人及你
无人及你 2020-12-12 17:56

I want current time in following format in a string.

dd-mm-yyyy HH:MM

How?

4条回答
  •  一个人的身影
    2020-12-12 18:30

    Maybe this will be more readable :

        NSDateFormatter *date = [[NSDateFormatter alloc] init];
        [date setDateFormat:@"HH:mm"];
        NSString *dateString = [date stringFromDate:[NSDate date]];
        [self.time setText:dateString];
    

    First of all we create an NSDateFormatter built-in in obj-c with the name date, then we apply it by [[NSDateFormatter alloc] init]; . After that we say to the code procesor that we want our date to have HOUR/MINUTE/SECOND. Finally we should make our date to be an string to work with alert or set value of a label , to do this we should create an string with NSString method then we use this : [date stringFromDate:[NSDate date]]

    Have Fun with It .

提交回复
热议问题