NSDateFormatter time format depending on Locale

后端 未结 2 1214
既然无缘
既然无缘 2020-12-19 19:03

I\'m experiencing a really strange issue that sounds more like a system bug. I want to format a date using only Hour and Minute information and, if necessary, display AM/PM

相关标签:
2条回答
  • 2020-12-19 19:31

    Swift 3 & 4:

    extension Date {
      var localizedStringTime: String {
        return DateFormatter.localizedString(from: self, dateStyle: .none, timeStyle: .short)
      }
    }
    
    0 讨论(0)
  • 2020-12-19 19:48

    You can use the localized string from date function like this...

    extension NSDate {
        func localizedStringTime()->String {
            return NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle)
        }
    }
    
    0 讨论(0)
提交回复
热议问题