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! While you're at it, check out some resources Treehouse students have shared here.

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

iOS

Keith Michelson
Keith Michelson
3,395 Points

Time 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
Pierre Smith
11,842 Points

You must set your dateformater to formatter.timeStyle = .ShortStyle

Keith Michelson
Keith Michelson
3,395 Points

Thanks for replying... This is kind of working, just not showing the AM PM.

let dateString = "8:22"
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeZone = NSTimeZone(name: "America/Denver")

let date = formatter.dateFromString(dateString)
println("date: \(date!)") // date: 2014-10-09 14:22:00 +0000

formatter.dateFormat = "H:mm"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)") // formattedDateString: 8:22
Keith Michelson
Keith Michelson
3,395 Points

Kept 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.

let dateString = "8:22"
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm"
formatter.timeZone = NSTimeZone(name: "America/Denver")

let date = formatter.dateFromString(dateString)
formatter.timeStyle = .ShortStyle
println("date: \(date!)") // date: 2014-10-09 14:22:00 +0000

formatter.dateFormat = "H:mm"
formatter.timeStyle = .ShortStyle
formatter.timeZone = NSTimeZone(name: "America/Denver")
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)") // formattedDateString: 8:22 AM
Keith Michelson
Keith Michelson
3,395 Points

I'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
Keith Michelson
3,395 Points

I guess I could do this NSTimeZone.localTimeZone().secondsFromGMT / 60 / 60