mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Change The Time Zone Offset Of A DateTime Object as a rails til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_1060 TILs and counting..._
|
_1061 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -638,6 +638,7 @@ _1060 TILs and counting..._
|
|||||||
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
||||||
- [Cast Common Boolean-Like Values To Booleans](rails/cast-common-boolean-like-values-to-booleans.md)
|
- [Cast Common Boolean-Like Values To Booleans](rails/cast-common-boolean-like-values-to-booleans.md)
|
||||||
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
||||||
|
- [Change The Time Zone Offset Of A DateTime Object](rails/change-the-time-zone-offset-of-a-datetime-object.md)
|
||||||
- [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md)
|
- [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md)
|
||||||
- [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md)
|
- [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md)
|
||||||
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
||||||
|
|||||||
27
rails/change-the-time-zone-offset-of-a-datetime-object.md
Normal file
27
rails/change-the-time-zone-offset-of-a-datetime-object.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Change The Time Zone Offset Of A DateTime Object
|
||||||
|
|
||||||
|
Let's say you have a timestamp string that you parse with
|
||||||
|
[`DateTime.parse`](https://ruby-doc.org/stdlib-2.6.1/libdoc/date/rdoc/DateTime.html#method-c-parse).
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> DateTime.parse('2021-02-23T11:59:11')
|
||||||
|
#=> Tue, 23 Feb 2021 11:59:11 +0000
|
||||||
|
```
|
||||||
|
|
||||||
|
Without the specification of a time zone offset in the timestamp string, it
|
||||||
|
will be parsed as UTC.
|
||||||
|
|
||||||
|
If you want to change it to another time zone, you can alter the `offset`
|
||||||
|
option of the `DateTime` object. Rails provides the
|
||||||
|
[`#change`](https://api.rubyonrails.org/classes/DateTime.html#method-i-change)
|
||||||
|
method for doing this.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> DateTime.parse('2021-02-23T11:59:11').change(offset: '-600')
|
||||||
|
#=> Tue, 23 Feb 2021 11:59:11 -0600
|
||||||
|
```
|
||||||
|
|
||||||
|
By changing the `offset` to `-600`, the `DateTime` now represents a time in
|
||||||
|
Central Time.
|
||||||
|
|
||||||
|
[source](https://stackoverflow.com/a/47861810/535590)
|
||||||
Reference in New Issue
Block a user