I have one model with the name job
.
I want to calculate the number of days since the job
has been created.
I think we have to calculate the difference between Date.today
and jobs
created_at
my method is:
def self.open_jobs(date)
Date.today - where("status='open' AND date(created_at) = ?",date).round
end
I am getting some error please let me know how to calculate the days the job has been created.
I have to calculate number of days. for example I have created a job on 20th sep 2012. it should be Date.today - 20/09/2013 answer should be 5 days and one more thing i need to check one more column status ="open" and display only those jobs.
if anybody knows how to calculate please post here.
job.created_at
is a Time
, Date.today
is a Date
You can't subtract Time
and Date
. See that Date.tomorrow - Time.now
gives you that error.
To get the difference in seconds:
Time.now - job.created_at
You can convert this to days (float or integer) as follows:
(Time.now - job.created_at) / 24.hours
((Time.now - job.created_at) / 24.hours).to_i
Alternately there is (MyGod's answer):
Date.today.mjd - job.created_at.to_date.mjd
You're right that you should use Date.today
and job.created_at
If you want to calculate the number of days since the job has been created use this:
def job_for_days
(Date.today - self.created_at).round(1.day)
end
Given two dates-
date1= Date.parse("10/10/2012")
date2= Date.parse("10/21/2012")
The difference in days:
date2.mjd - date1.mjd
where,
mjd = Modified Julian Day Number
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