Created
June 19, 2024 15:18
-
-
Save coolprobn/67c3c3d58a74a7af15559a05d980aa9c to your computer and use it in GitHub Desktop.
Convert any DateTime to the timezone you prefer while still keeping the same date and time - Ruby on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def date_in_danish_time(date_time) | |
date_array = | |
date_time.strftime("%Y %m %d").split.map { |string| Integer(string, 0) } | |
danish_date = Date.new(*date_array).in_time_zone("Copenhagen") | |
# extracting offset from the date will help us in also taking care of Daylight Saving | |
danish_date_offset = danish_date.formatted_offset | |
date_time_array = | |
date_time | |
.strftime("%Y %m %d %H %M %S") | |
.split | |
.map { |string| Integer(string, 0) } | |
DateTime.new(*date_time_array, danish_date_offset) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment