Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Keith Michelson
3,395 PointsTime Offset and AM PM in Swift
I have military times in a database that I want to convert to AM - PM in swift. Any ideas or recommended ways to handle time like this?
Thanks for any help.
4 Answers

Pierre Smith
11,842 PointsYou must set your dateformater to formatter.timeStyle = .ShortStyle

Keith Michelson
3,395 PointsI've been working with that a little bit, this is what i've found but just can't quite get it to just do the time with the A.M. P.M.
let dateString = "6:33"
let formatter = NSDateFormatter()
formatter.dateStyle = .NoStyle
formatter.timeStyle = .ShortStyle
formatter.dateFormat = "HH:mm"
formatter.timeZone = NSTimeZone(name: "America/Denver")
let date = formatter.dateFromString(dateString)
println("date: \(date!)") // date: 2014-10-09 12:33:00 +0000

Keith Michelson
3,395 PointsI guess I could do this NSTimeZone.localTimeZone().secondsFromGMT / 60 / 60

Keith Michelson
3,395 PointsI got help on stackoverflow, this worked perfectly. http://stackoverflow.com/questions/27612785/adding-time-offset-in-swift/27613605#27613605
Keith Michelson
3,395 PointsKeith Michelson
3,395 PointsThanks for replying... This is kind of working, just not showing the AM PM.
Keith Michelson
3,395 PointsKeith Michelson
3,395 PointsKept playing with it, it's doing the AM PM with that now using the timestyle like you said, thank you. Now i'm just trying to figure out the offset if a user is in a different time zone to pull from the database time but display the correct time to them with their offset.