Difference between DateTime and Time in Ruby

前端 未结 7 1057
眼角桃花
眼角桃花 2020-11-27 10:03

What\'s the difference between DateTime and Time classes in Ruby and what factors would cause me to choose one or the other?

相关标签:
7条回答
  • 2020-11-27 10:41

    Consider how they handle timezones differently with custom instantiations:

    irb(main):001:0> Time.new(2016,9,1)
    => 2016-09-01 00:00:00 -0400
    irb(main):002:0> DateTime.new(2016,9,1)
    => Thu, 01 Sep 2016 00:00:00 +0000
    irb(main):003:0> Time.new(2016,9,1).to_i
    => 1472702400
    irb(main):004:0> DateTime.new(2016,9,1).to_i
    => 1472688000
    

    This can be tricky when creating time ranges, etc.

    0 讨论(0)
提交回复
热议问题