HomeiOS Developmentios - convert HH:mm string to gmt time string HH:mm

ios – convert HH:mm string to gmt time string HH:mm


I am utilizing this code to transform HH:mm string to gmt time string HH:mm:

func convertToGMT(timeString: String) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        
        // Assuming the enter time is within the consumer's native time zone
        dateFormatter.timeZone = TimeZone.present
        
        // Parse the enter time string right into a Date object
        guard let date = dateFormatter.date(from: timeString) else {
            return "" // Return nil if the enter format is invalid
        }
        
        // Convert the date to GMT (UTC) time zone
        dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
        let gmtTimeString = dateFormatter.string(from: date)
        
        return gmtTimeString
    }

The issue is that’s Jerusalem gmt -3 i get for 12:00 -> 10:00 as a substitute of 09:00.

What’s the downside?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments