복붙노트

[SWIFT] 지역에 따라 NSDateFormatter 시간 형식

SWIFT

지역에 따라 NSDateFormatter 시간 형식

해결법


  1. 1.이 같은 날짜 함수에서 지역화 된 문자열을 사용할 수 있습니다 ...

    이 같은 날짜 함수에서 지역화 된 문자열을 사용할 수 있습니다 ...

    extension NSDate {
        func localizedStringTime()->String {
            return NSDateFormatter.localizedStringFromDate(self, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle)
        }
    }
    

  2. 2.스위프트 3 & 4 :

    스위프트 3 & 4 :

    extension Date {
      var localizedStringTime: String {
        return DateFormatter.localizedString(from: self, dateStyle: .none, timeStyle: .short)
      }
    }
    
  3. from https://stackoverflow.com/questions/28722309/nsdateformatter-time-format-depending-on-locale by cc-by-sa and MIT license