wrong time using NSDateFormatter

I know there is several question about this. But i tried all the recommendation and still the time doesn't match the time atonmy real device.

    let dater = NSDate()
    let dayTimePeriodFormatter = NSDateFormatter()
    dayTimePeriodFormatter.locale = NSLocale.currentLocale()
    dayTimePeriodFormatter.timeZone = NSTimeZone(name: "UTC")
    dayTimePeriodFormatter.dateFormat = "H:s" 
    var dateString = dayTimePeriodFormatter.stringFromDate(dater)

Device(real) time : 22:11 . dateString output : 00:36

Any suggestions? Thanks!

Answers


As Nick points out, you're using UTC as your time zone, and you can get to the local time zone with NSTimeZone.systemTimeZone()

It's also likely that you don't really intend to use just the hours (H) and seconds (s) You probably also want the minutes (m). Try using a dateFormat of "H:m:s"

Better yet, use "H:mm:ss" which will force the minutes and seconds to be two digits, as opposed to just one, so you don't wind up with "10:4:3"


Have you tried dayTimePeriodFormatter.dateFormat = "HH:mm"?

Apple uses fixed Internet. Check out here to see some examples. This may be helpful


Need Your Help

How to hide Application Window when its EXE has been called from a Process?

c# .net .net-services

I'm running an application's EXE using a Class Library from a Service.

How to add current time in an excel document for range of cell

excel vba excel-2010

I would like to add current system time in an excel document when i should click on the cell.