3.days.ago, 2.hours.from_now etc without Rails?
I'm not sure if there's another gem available besides ActiveSupport
, but it would be really straight-forward to make a small version yourself:
class Fixnum
SECONDS_IN_DAY = 24 * 60 * 60
def days
self * SECONDS_IN_DAY
end
def ago
Time.now - self
end
end
3.days.ago #=> 2011-06-18 08:45:29 0200
from_now
can be implemented like ago
but with + self
and weeks
, hours
etc. like days
using different constants.
How to add 10 days to current time in Rails
Use
Time.now + 10.days
or even
10.days.from_now
Both definitely work. Are you sure you're in Rails and not just Ruby?
If you definitely are in Rails, where are you trying to run this from? Note that Active Support has to be loaded.
1 week from now at 8 o'clock
There are many ways to achieve this. Here's one:
Time.parse('8am') + 1.week
Here's another:
DateTime.now.beginning_of_day + 1.week + 8.hours
Or how about:
1.week.from_now.beginning_of_day + 8.hours
Or even:
DateTime.now.advance(days: 7).change(hour: 8)
Ruby (with Rails) convert a string of time into seconds?
What you're asking Ruby to do with Time.parse is determine a time of day. That's not what you are wanting. All of the libraries I can think of are similar in this aspect: they are interested in absolute times, not lengths of time.
To convert your strings into time formats that we can work with, I recommend using Chronic (gem install chronic
). To convert to seconds, we can do everything relative to the current time, then subtract that time to get the absolute number of seconds, as desired.
def seconds_in(time)
now = Time.now
Chronic.parse("#{time} from now", :now => now) - now
end
seconds_in '48 hours' # => 172,800.0
seconds_in '15 minutes' # => 900.0
seconds_in 'a lifetime' # NoMethodError, not 42 ;)
A couple quick notes:
- The
from now
is is why Chronic is needed — it handles natural language input. - We're specifying
now
to be safe from a case where Time.now changes from the time that Chronic does it's magic and the time we subtract it from the result. It might not occur ever, but better safe than sorry here I think.
How to calculate time ago in Java?
Take a look at the PrettyTime library.
It's quite simple to use:
import org.ocpsoft.prettytime.PrettyTime;
PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
// prints "moments ago"
You can also pass in a locale for internationalized messages:
PrettyTime p = new PrettyTime(new Locale("fr"));
System.out.println(p.format(new Date()));
// prints "à l'instant"
As noted in the comments, Android has this functionality built into the android.text.format.DateUtils
class.
Ruby or Rails find records dated two months from now
You can add the following scope
to your model:
scope :expiring_soon, -> {
date = Date.today >> 1
where(expires_at: (date.beginning_of_month..date.end_of_month))
}
And then call the scope like this:
YourModel.expiring_soon
You might want to read about Date/Time calculations and Range queries (see 2.3.2) in Rails.
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
Use example :
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
Input can be any supported date and time format.
Output :
4 months ago
4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Function :
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Check if a date is 24 hours old
Add this const oneday = 60 * 60 * 24 * 1000 (milliseconds)
. Since, JS converts difference between 2 Datetime objects into milliseconds. I think that should work
Related Topics
Sass: Dealing with The Ie 4095 Selectors Per Stylesheet Restriction
Ruby on Rails Triggers Update on Serialized Attribute Every Time
Benefits of Using 'Hash#Fetch' Over 'Hash#[]'
Preventing Delayed_Job Background Jobs from Consuming Too Much CPU on a Single Server
What Is The Logic Behind This Result
How to Get Ruby-Debug-Ide to Work
Cancan Load_And_Authorize_Resource Triggers Forbidden Attributes
How to Do a Before_Action in Ruby (Like in Rails)
Using Ruby 2.0 on Amazon Opsworks
How to Create a Dynamic Regular Expression in Ruby
Rails Elasticsearch Aggregation
Ruby Inject with Index and Brackets
Alternative for Accepts_Nested_Attributes_For - Maybe Virtus
How to Properly Test Cancan Abilities with Rspec
Generate a 'Link_To' to the Controller Action 'Edit', Dynamically