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?
Ruby | DateTime now() function DateTime#now() : now() is a DateTime class method which returns a DateTime object denoting the given calendar date. Return: DateTime object denoting the given calendar date.
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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With