To Determine if Ruby Has a Time Object Within 24 Hours

Asked 2 years ago, Updated 2 years ago, 37 views

How can ruby determine if the DateTime object is within 24 hours?

I'd like to do time=time.zone.now and then go see time to determine if it's within 24 hours.

ruby-on-rails ruby

2022-09-30 19:39

2 Answers

time=Time.zone.now
time>1.day.ago

In a Rails environment, you can write like this.
I think it will be easier to read colloquially.

http://railsdoc.com/references/ago


2022-09-30 19:39

It looks like a Rails environment, so it's easy to use 1.day.

(Time.zone.now-time)<1.day

true if the time specified in is within 1 day (=24 hours).
Of course, 24.hours can be used instead of 1.day.


2022-09-30 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.