Hi
I'm in need of getting a localized Date. Here is some of my code so far for getting a date (this is from a ble characteristic, but that probably doesn't matter).
let timeNow: NSDate = NSDate()
let negMinutes = -1*Int(timestamps[0])
let dateStarted: NSDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.Minute, value: negMinutes, toDate: timeNow, options: [])!
print("\n Date/Time UTC Started: \(dateStarted)")
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
dateFormatter.timeStyle = NSDateFormatterStyle.LongStyle
let dateStartedLocal = dateFormatter.stringFromDate(dateStarted!)
print(" Date/Time Started Local Str: \(dateStartedLocal)")
dateFormatter.locale = NSLocale.currentLocale()
let localStartDate: NSDate = dateFormatter.dateFromString(dateStartedLocal)!
print(" Date/Time Started NSDate: \(localStartDate)\n")
The output for the above code is:
Date/Time UTC Started: Optional(2016-10-17 11:40:29 +0000)
Date/Time Started Local Str: October 17, 2016 at 7:40:29 AM EDT
Date/Time Started NSDate: 2016-10-17 11:40:29 +0000
The first date/time is UTC which then gets converted to local date/time. But when I try to generate an NSDate from the localized date string it goes back to UTC. I want it to stay as the localized date, but as an NSDate. Any idea what I'm doing wrong?
I'm also curious what that "Optional" part of the first date/time output is, but it may not be important.
Thanks...