Represent a time with no date in ruby

2019-01-11 13:52发布

Is there a way to represent a time in ruby without having a Date attached? I am doing a timetracking application and I only need in/out times, there is a separate column for date.

The MySQL TIME data type stores only the time, but in Rails it comes back as Jan 1 2000 + what ever time is there.

Should i just ignore the date part, or is there a way to eliminate it?

2条回答
来,给爷笑一个
2楼-- · 2019-01-11 14:26

There's no way to eliminate it, because:

Time is stored internally as the number of seconds with fraction since the Epoch, January 1, 1970 00:00 UTC.

Just format it like this:

t = Time.now
t.strftime("at %I:%M%p")
查看更多
走好不送
3楼-- · 2019-01-11 14:44

We just store times with no attached dates as minutes since midnight in an integer column (that's Postgres not MySQL but nonetheless). Maybe that approach can work for you too?

查看更多
登录 后发表回答