Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: get the offset value from a date object

Tags:

date

swift

as simple as it sounds, but it is hard to find my exact question in google. I'm trying to ignore the UTC printed out value. I receive multiple dates, this one here is just an example: (it could be +0900, -0200, etc...)

"2017-05-01T12:30:00-0700"

once I apply it to a value using these lines:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxxx"
if let result = formatter.date(from: time) {print result}

the value of the dateTimeResult prints:

2017-05-01 19:30:00 UTC

Using swift date objects, how do I slice out the part "-0700", multiply the -7 or +7 (this example is negative) by minutes by seconds. I'll save that total as int in DB (I need it for categorizing the different timezones later). Then applying that total to the incoming date input using this line:

let output = Calendar.current.date(byAdding: .second, value: totalSecs, to: result)

The goal is to end up with this date:

"2017-05-01 12:30:00"

I already have a solution using string manipulation, but I don't think that is the ideal solution. If it must be done by string, how do you do it?

like image 548
user1019042 Avatar asked Oct 15 '25 14:10

user1019042


2 Answers

If I understand you correctly you want only the date and time portion ignoring always the time zone information.

In this case strip the time zone from the date string with regular expression

let dateString = "2017-05-01T12:30:00-0700"
let dateStringIgnoringTimeZone = dateString.replacingOccurrences(of: "[+-]\\d{4}", with: "", options: .regularExpression)
print(dateStringIgnoringTimeZone) // "2017-05-01T12:30:00"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let date = dateFormatter.date(from: dateStringIgnoringTimeZone)!
like image 192
vadian Avatar answered Oct 18 '25 10:10

vadian


I think you should keep the date as it is and then just use DateFormatter to display the time at that timezone

let time = "2017-05-01T12:30:00-0700"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxxx"
if let result = dateFormatter.date(from: time) {
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    print(dateFormatter.string(from: result))  // "2017-05-01 16:30:00   (corresponding time at my location GMT-3)
    // to display it at -0700 just set the formatter timaZone
    dateFormatter.timeZone = TimeZone(secondsFromGMT: -3600 * 7)
    print(dateFormatter.string(from: result))  // "2017-05-01 12:30:00\n"
}

To get the timezone offset from the string:

let hours = Int(time.suffix(5).prefix(3)) ?? 0
let minutes = Int(time.suffix(2)) ?? 0
let offset = hours * 3600 + minutes * 60 
print(offset)   // -25200
like image 26
Leo Dabus Avatar answered Oct 18 '25 09:10

Leo Dabus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!