0

I have the code below to get date from <%= form.date_select :date %> and compare if any post exists with the same date.

    # Get the date from form and format it to compare with db
    # date(1i)"=>"2021", "date(2i)"=>"3", "date(3i)"=>"22"
    @selected_date = Date.new(params["post"]["date(1i)"].to_i,
                              params["post"]["date(2i)"].to_i,
                              params["post"]["date(3i)"].to_i)

    # Check if the post exists in the db
    @existing_post = Post.where(user_id: current_user.id, date: @selected_date).first

This works well. Now I need to do that same with the date from email.

@selected_date = mail.date.to_s

But this mail date is in different format and comparing to date in db always fails. I can't use the ["date(1i)"] with mail.date.

How can I format this email.date so that I can compare the existence of it to date field in db?

Thank you


Update: Date format that comes from email as below I guess. Looking at the Logs on server

Date: Wed, 24 Mar 2021 09:57:57 +0000 enter image description here

Date format I have in the DB is as below. Output of Post.last in rails c

date: "2021-03-24 09:57:57.000000000 +0000 enter image description here

Designer
  • 1,061
  • 1
  • 12
  • 26
  • Where does `mall` come from ? `mail.date` isn't already what you need (without the `to_s`)? – Fravadona Mar 23 '21 at 23:32
  • Provide example format of date(1i) and mail.date, So that question can be more clear and we can help you if possible. – honey Mar 24 '21 at 04:06
  • @Fravadona I tried that, it doesn't work. I guess I need to match only the date part without time. Btw I added date screenshots to the post above – Designer Mar 24 '21 at 10:33
  • @honey hi, thank you. I just added screenshots to the post – Designer Mar 24 '21 at 10:34

1 Answers1

0
  mail.date.to_time == date.to_time

If above statement isn't work then try

  mail.date.to_s.to_time == date.to_s.to_time

If you execute the following statement you will get the correct answer, try it in your console

  "Wed, 24 Mar 2021 09:57:57 +0000".to_time == "2021-03-24 09:57:57.000000000 +0000".to_time
honey
  • 981
  • 6
  • 19