Comparing dates not working for different years but working fine for same year
up vote
-3
down vote
favorite
I want to compare the following dates with different years and saving status:
@date = DateTime.now.strftime("%d-%b-%Y")
@valid_to_date = @new_certificate.valid_to.strftime("%d-%b-%Y")
if @valid_to_date == @date
@new_certificate.update_attributes(status: 1)
elsif @valid_to_date > @date
@new_certificate.update_attributes(status: 2)
elsif @valid_to_date < @date
@new_certificate.update_attributes(status: 3)
else
end
It's working fine for the same years, but for different years, it is not working. Please help me to solve this.
ruby ruby-on-rails-5
add a comment |
up vote
-3
down vote
favorite
I want to compare the following dates with different years and saving status:
@date = DateTime.now.strftime("%d-%b-%Y")
@valid_to_date = @new_certificate.valid_to.strftime("%d-%b-%Y")
if @valid_to_date == @date
@new_certificate.update_attributes(status: 1)
elsif @valid_to_date > @date
@new_certificate.update_attributes(status: 2)
elsif @valid_to_date < @date
@new_certificate.update_attributes(status: 3)
else
end
It's working fine for the same years, but for different years, it is not working. Please help me to solve this.
ruby ruby-on-rails-5
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I want to compare the following dates with different years and saving status:
@date = DateTime.now.strftime("%d-%b-%Y")
@valid_to_date = @new_certificate.valid_to.strftime("%d-%b-%Y")
if @valid_to_date == @date
@new_certificate.update_attributes(status: 1)
elsif @valid_to_date > @date
@new_certificate.update_attributes(status: 2)
elsif @valid_to_date < @date
@new_certificate.update_attributes(status: 3)
else
end
It's working fine for the same years, but for different years, it is not working. Please help me to solve this.
ruby ruby-on-rails-5
I want to compare the following dates with different years and saving status:
@date = DateTime.now.strftime("%d-%b-%Y")
@valid_to_date = @new_certificate.valid_to.strftime("%d-%b-%Y")
if @valid_to_date == @date
@new_certificate.update_attributes(status: 1)
elsif @valid_to_date > @date
@new_certificate.update_attributes(status: 2)
elsif @valid_to_date < @date
@new_certificate.update_attributes(status: 3)
else
end
It's working fine for the same years, but for different years, it is not working. Please help me to solve this.
ruby ruby-on-rails-5
ruby ruby-on-rails-5
edited yesterday
sawa
128k27191296
128k27191296
asked yesterday
Mohammed Riyaz
15
15
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
If you want to compare dates as dates, and not as the specific string format that you wrote, then compare them as dates:
@date = Date.today
@valid_to_date = @new_certificate.valid_to.to_date
Even if you chose to compare the dates as strings, the comparison goes from the left to right (unless you are using a language like Arabic), so your string format will obviously not work. You also would need padding by zero in appropriate places.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you want to compare dates as dates, and not as the specific string format that you wrote, then compare them as dates:
@date = Date.today
@valid_to_date = @new_certificate.valid_to.to_date
Even if you chose to compare the dates as strings, the comparison goes from the left to right (unless you are using a language like Arabic), so your string format will obviously not work. You also would need padding by zero in appropriate places.
add a comment |
up vote
1
down vote
If you want to compare dates as dates, and not as the specific string format that you wrote, then compare them as dates:
@date = Date.today
@valid_to_date = @new_certificate.valid_to.to_date
Even if you chose to compare the dates as strings, the comparison goes from the left to right (unless you are using a language like Arabic), so your string format will obviously not work. You also would need padding by zero in appropriate places.
add a comment |
up vote
1
down vote
up vote
1
down vote
If you want to compare dates as dates, and not as the specific string format that you wrote, then compare them as dates:
@date = Date.today
@valid_to_date = @new_certificate.valid_to.to_date
Even if you chose to compare the dates as strings, the comparison goes from the left to right (unless you are using a language like Arabic), so your string format will obviously not work. You also would need padding by zero in appropriate places.
If you want to compare dates as dates, and not as the specific string format that you wrote, then compare them as dates:
@date = Date.today
@valid_to_date = @new_certificate.valid_to.to_date
Even if you chose to compare the dates as strings, the comparison goes from the left to right (unless you are using a language like Arabic), so your string format will obviously not work. You also would need padding by zero in appropriate places.
edited yesterday
answered yesterday
sawa
128k27191296
128k27191296
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224401%2fcomparing-dates-not-working-for-different-years-but-working-fine-for-same-year%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password