简体   繁体   中英

Why Ruby's strftime is rounding up the date?

I have a Ruby on Rails project. Let's say I have a table items in my database with a column called expiry_date .

Consider these two records from the items table:

  1. id: 1, expiry_date: 2020-12-25 23:29:00 (assume that I loaded this into the instance variable @item_one as an ActiveRecord object)
  2. id: 2, expiry_date: 2020-12-25 12:29:00 (assume that I loaded this into the instance variable @item_two as an ActiveRecord object)

Why did the strftime method gives me this (I expect both of them to evaluate to 25 December 2020 ?

@item_one.expiry_date.strftime('%-d %B %Y') evaluates to 26 December 2020 @item_two.expiry_date.strftime('%-d %B %Y') evaluates to 25 December 2020

I figured it out in the end.

The timezone of my machine is GMT+8.

The dates in my question was directly taken from the database and when I strftime d it adds 8 hours to the time part, hence rounding up the date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM