How to convert 24 hr String time into 12 hr time String format

后端 未结 3 1377
清酒与你
清酒与你 2021-02-04 09:25

I have time in NSString like \"15:15\"

I want to covert this NSString time into 12 hr NSString(i.e \"3:15 PM\").

相关标签:
3条回答
  • 2021-02-04 10:03

    @python

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:locale];
    [dateFormatter setDateFormat:@"hh:mm"];
    

    Hope this helps!

    0 讨论(0)
  • 2021-02-04 10:12

    Use NSDateFormatter to turn the string in a NSDate. Then use the dateformatter again to change the NSDate to a string.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"HH:mm";
    NSDate *date = [dateFormatter dateFromString:@"15:15"];
    
    dateFormatter.dateFormat = @"hh:mm a";
    NSString *pmamDateString = [dateFormatter stringFromDate:date];
    

    Code might contain some errors, written without compiling or testing.

    0 讨论(0)
  • 2021-02-04 10:22

    Check out this SO question: NSDateFormatter dateFromString and iPhone in 24 Hour Format Confusion

    You should be able to use to use NSDateFormatter to format the style of time you want, then read in the string to the formatter and pull it back out in your desired format.

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