How to get the time in am/pm format iphone NSDateFormatter?

后端 未结 8 1101
一生所求
一生所求 2020-12-13 05:44

Am working in an iPhone app with using the NSDateFormatter. Am getting the time from the webservice like \"00:00:00\", \"16:00:00\",\"15:30:00\" and so on. I ne

相关标签:
8条回答
  • 2020-12-13 06:34

    This Below code always keep in 12hour format:

    NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    df.locale = 12hourFomrat;
    

    This always keep in 24 hour format

    NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
    df.locale = 24hourFormat
    
    0 讨论(0)
  • 2020-12-13 06:41

    Hope someone would be helpful who would like to get it in Swift

    Swift 5.1+

    let dateFormatter = DateFormatter()
    //if you want 12AM,5PM then use "hh mm a"
    //if you want 00AM,17PM then use "HH mm a"
    dateFormatter.dateFormat = "hh mm a"
    let stringDate = dateFormatter.string(from: Date())
    print(stringDate)
    
    0 讨论(0)
提交回复
热议问题