1

I had set time to San Carlos California(PDT Timezone) in my I phone, then I am trying to add 30 days which is fine , but when I am trying to convert that Date object to IST Timezone , it is not correct.

11/19/2020 06:46:25 Above one is days added by 30 in todays date, In San Calos(PDT Timezone) when I am converting that time in IST it is "11/18/2020 at 11:46 PM" but it should be "11/18/2020 at 19:46"

time is variant..

I am using this to convert time into IST

Convert date to local TimeZone

func convertExpiryToIst(inputFormat: String, outputFormat: String) -> String{
    let formatter = DateFormatter()
    formatter.dateFormat = inputFormat
    formatter.timeZone = TimeZone.current
    var date = formatter.date(from: self)   
    date?.addTimeInterval(TimeInterval(value))
    formatter.dateFormat = outputFormat
    if date != nil {
        print(date)
        return formatter.string(from: date!)
    }
    return self
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
relinnse
  • 21
  • 2

1 Answers1

0

The issue there is that you are adding a time interval to the resulting date. A date has no timezone it is just a point in time. All you need to do is to change the date formatter timezone to the desired one and get the string representation of it.

extension String {
    func convertExpiryTo(inputFormat: String, inputTimeZone: String, outputFormat: String,  outputTimeZone: String)-> String? {
        let formatter = DateFormatter()
        formatter.dateFormat = inputFormat
        formatter.timeZone = TimeZone(identifier: inputTimeZone)
        if let date = formatter.date(from: self) {
            // let offset = TimeZone(abbreviation: "IST")!.secondsFromGMT(for: date) -  TimeZone(abbreviation: "PDT")!.secondsFromGMT(for: date)
            // let hours = Double(offset) / 3600   // 13.5 hours
            // dont add the timezone offset
            // date?.addTimeInterval(TimeInterval(value))
            formatter.dateFormat = outputFormat
            // just set a different  timezone
            formatter.timeZone = TimeZone(identifier: outputTimeZone)
            return formatter.string(from: date)
        }
        return nil
    }
}

"11/19/2020 06:46:25".convertExpiry(inputFormat: "MM/dd/yyyy HH:mm:ss", inputTimeZone: "America/Los_Angeles", outputFormat: "MM/dd/yyyy 'at' h:mm a", outputTimeZone: "Asia/Kolkata")  // "11/19/2020 at 8:16 PM" = +13.5 hours
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 11/20/2020 at 10:22 PM 11/21/2020 at 11:52 AM It should be one hour behind that is. 11/21/2020 at 10:52 AM – relinnse Oct 22 '20 at 05:23
  • I had tried to convert that but my time conversion from PDT TO IST returns me time one hour ahead of actual time 11/20/2020 at 10:22 PM 11/21/2020 at 11:52 AM It should be one hour behind that is. 11/21/2020 at 10:52 AM @Leo Dabus – relinnse Oct 22 '20 at 05:24
  • The timezone offset depends on the date. `TimeZone(abbreviation: "IST")!.secondsFromGMT(for: date)` – Leo Dabus Oct 22 '20 at 05:28
  • I am facing this issue due to addition of 30 days in current date for e.g its 22 oct 2020 in India today, but after adding 30 days to it .. date will switch to November... which in PDT refers to one hour ahead ...(becuase from nov , clock in pet set to one hour ahead this is the issue ). – relinnse Oct 22 '20 at 05:35
  • There is no issue. Don't mess with the timezone. The code above is all you need, – Leo Dabus Oct 22 '20 at 05:37
  • How are you adding 30 days to the date? Do you want to add 30 days or a month? – Leo Dabus Oct 22 '20 at 05:38
  • by adding 30 days in date object... its perfectly fine the issue is of Daylight saving .. like in America clock set twice in nov and in mar .. but in India there is nothing like Daylight .. so due to that for months in between mar to oct 31 its is giving me correct time but after 1 nov to feb 28 it is making my time ahead by one hour – relinnse Oct 22 '20 at 05:50
  • @relinnse if your timezone is what matters why are you using that location time? You need to be more clear. A date has no timezone. It is just a point in time. – Leo Dabus Oct 22 '20 at 05:53
  • I am not sure what you are trying to accomplish but this might help. https://stackoverflow.com/a/62590112/2303865 or https://stackoverflow.com/a/46660225/2303865 – Leo Dabus Oct 22 '20 at 05:56