-1

I have a requirement in which I have a local DB full of dates from a particular timezone and I want to convert this date from that particular timezone to user's local timezone. The way I've implemented is that first I am always converting the DB date to UTC by adding the hours difference and then converting it to user local time zone by taking the time interval using

    NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT

and adding it to the UTC date. I would like to know that is the approach fine because it worked till now on my limited testing. Also will it account for countries which has Day light saving currently active.

The complete code :

    func calculateDate(model:EventModel) -> NSDate
{
    let date = model.Date
    let startTime = model.StartTime
    let arrayForTime = startTime?.componentsSeparatedByString(":")
    let arrayForDates = date?.componentsSeparatedByString("-")

    let calender = NSCalendar(identifier:NSCalendarIdentifierGregorian)
    let year = Int(arrayForDates![2])
    let month = Int(arrayForDates![1])
    let day = Int(arrayForDates![0])
    let hour = Int(arrayForTime![0])! + 3  //UTC - 3 the local time
    let minutes = Int(arrayForTime![1])

    let dateComponents = NSDateComponents()
    dateComponents.day = day!
    dateComponents.month = month!
    dateComponents.year = year!
    dateComponents.hour = hour
    dateComponents.minute = minutes!
    dateComponents.second = 0
    dateComponents.timeZone = NSTimeZone(name: "UTC")
    let UTCDate = calender?.dateFromComponents(dateComponents)
    let dateLocal = self.getLocalDate(UTCDate!)

    return dateLocal
}

    func getLocalDate(utcDate:NSDate) -> NSDate 
{
    let timeInterval = NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT)
    let localdate = utcDate.dateByAddingTimeInterval(timeInterval)
    return localdate
}

Previously I was using this but it was not returning the correct local date for Day light saving countries.

    func getLocalDate(utcDate:NSDate) -> NSDate
{
    let timeInterval =   NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT)
    //        let timeZoneObj = NSTimeZone.localTimeZone()
    let localdate = utcDate.dateByAddingTimeInterval(timeInterval)
    //        let isDayLightSavingOn =   timeZoneObj.isDaylightSavingTimeForDate(localdate)
    //        if(isDayLightSavingOn == true)
    //        {
    //            let dayLightTimeInterval =  timeZoneObj.daylightSavingTimeOffsetForDate(localdate)
    //            timeInterval -= dayLightTimeInterval
    //        }
    //        localdate = utcDate.dateByAddingTimeInterval(timeInterval)
    return localdate
}

Thanks in advance. Cheers !

Maddiee
  • 288
  • 2
  • 11
  • 1
    You can probably do this much more simply with an NSDateFormatter. What does `startTime` look like? What Is the Timezone of the stored dates? – Paulw11 May 17 '16 at 20:52
  • Search Stack Overflow for "convert nsdate to local timezone" and you'll get many, many hits. – Rob May 18 '16 at 00:14

1 Answers1

0

You don't "convert" the NSDate to a timezone. You simply create a string representation of the NSDate in the user's time zone using a NSDateFormatter. The timeZone of the NSDateFormatter defaults to the user's own timezone, so no adjustments are necessary when displaying it in the user's local timezone. For example:

let date = ... // let's imagine it was "2016-05-18 00:03:34 +0000"

let formatter = NSDateFormatter()
formatter.dateStyle = .LongStyle
formatter.timeStyle = .LongStyle
let userDateString = formatter.stringFromDate(date)

If the user was in GMT/UTC-7 timezone, for example, that would result in:

"May 17, 2016 at 5:03:34 PM PDT"

Clearly, change the dateStyle and timeStyle to format it however best for your user interface. But don't do any adjustments of NSDate objects at all, but rather just build a string representation in the user's own timezone.

Rob
  • 415,655
  • 72
  • 787
  • 1,044