How can I convert the 24 hour time format to 12 hour time format?
I can get the 24 hour style time, but how do I get the 12 hour format with the am and pm?
let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Hour, .Minute, .Second], fromDate: date) let hour = components.hour let minutes = components.minute
- What is better place for loading data from API- viewDidLoad, viewWillAppear or viewDidAppear?
- Get a UILocalNotification to fire every week
- Get viewport height when soft keyboard is on
- Change font of back navigation bar button
- Possible to play video using a subclass of NSURLProtocol, using either MPMovieController or AVFoundation?
- Xcode 4 says “finished running <my app>” on the targeted device — Nothing happens
2 Solutions Collect From Internet About “How can I convert the 24 hour time format to 12 hour time format?”
As you say, you should use
NSDateFormatter for your desired format. Hope this will help you.
let date = NSDate() let formatter = NSDateFormatter() formatter.dateFormat = "hh:mm a" let time = formatter.stringFromDate(date)
For more information about date formatter see NSDateFormatter
Swift 3.1 Answer
let date = Date() //Jun 1, 2017, 10:22 AM" let formatter = DateFormatter() //<NSDateFormatter: 0x608000244380> formatter.dateFormat = "hh:mm a" //<NSDateFormatter: 0x608000244380> let time = formatter.string(from: date) //"10:22 AM"
- Defining Objective-C blocks as properties – best practice
- How do I decode HTML entities in swift 2.0?
- App with AVPlayer plays mp4 interrupt iPod music after launched
- How to implement SplitViewController on second level.?
- Auto Layout with relative constraints not affecting systemLayoutSizeFittingSize: for UITableViewCell
- A long-running operation is being executed on the main thread warning on regular Parse functions
- WatchOS2 WCSession How many delegates can the WCSession have in WatchKitExtension?
- Display the list of my twitter followers in ios 5 app?
- Using AFNetworking to post both text and multiple images to Google Blobstore
- Testflight Profile Installation Failed, the SCEP server return an invalid response
- When can I activate/deactivate layout constraints?
- What's the height of a UITabBar on iOS 8, iOS 9 and iOS 10?
- window.close() doesn't work on iOS 8 GM seed
- How to animate add UISearchBar on top of UINavigationBar
- How to compare two strings ignoring case in Swift language?