title: Swift how to get 12 hours format if system is 24 hour format
body: <p>My iPhone device system was set in 24hours format i want to get 12 hours format, that is:         <strong>23:27</strong> get <strong>11:27 PM</strong>.</p>
  
  <p>I tried:</p>
  
  <pre><code>            let today = NSDate()
              let dateFormatter = DateFormatter()
              dateFormatter.dateFormat = "hh:mm a"
              let string = dateFormatter.string(from: today as Date) // 23:27
  </code></pre>
  
  <p>got <code>23:27</code> string.</p>
  
  <p>I found this: <a href="https://stackoverflow.com/questions/6247790/how-to-get-12-hour-format-time-string-when-system-is-set-to-use-24-hour-format">How to get 12 hour format time string when system is set to use 24 hour format</a></p>
  
  <pre><code>            let today = NSDate()
              let dateFormatter = DateFormatter()
              let dateFormat = DateFormatter.dateFormat(fromTemplate:"hh:mm a", options: 0, locale: NSLocale.current)
              dateFormatter.dateFormat = dateFormat
              let string = dateFormatter.string(from: today as Date) //23:27 i want 11:27 pm
  </code></pre>
  
  <p>But it doesn't work i still got <code>23:27</code> string not <code>11:27pm</code> string.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT</strong>:
  According to <code>shallowThought</code>'s answer not sure if <code>Locale</code> is the case?</p>
  
  <p><a href="https://i.stack.imgur.com/H1JrQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H1JrQ.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT2</strong>
  Add this:</p>
  
  <pre><code>dateFormatter1.locale = Locale(identifier: "en_US_POSIX")
  </code></pre>
  
  <p>works. And my current locale is <code>en_CN</code>.</p>