Skip to content
Advertisement

Compare date of input type datetime with date in SQL using flask

I use this .html page to get input type datetime:

<div class="form-group full-input">
    <div class="row">
       <div class="col-6">
          <label>start</label>
          <input type="datetime-local" name="stime" required="">
          # stime: 2021-07-13 01:38:00
       </div>
     </div>
<div>

And my .py code:

@flask.route('/current_lecture')
def currtlec():
   today_date = datetime.today().strftime('%Y-%m-%d')
   print(today_date)
   # today_date: 2021-07-13
   cursor = mysql.connection.cursor()
   cursor.execute('SELECT * FROM `sessioncreate` WHERE sessioncreate.sdate = %s',(today_date))
   myresult = cursor.fetchall()
   return render_template("current-lecture.html", myresult=myresult)

my DB:

DB Example

I want to show only all data of ‘sessioncreate’ when only date of ‘sdate’ equal of today date

How can i achieve that?

Advertisement

Answer

Use the DATE() function. This way you can convert the DATETIME in your database to DATE and easily compare.

cursor.execute('SELECT * FROM sessioncreate WHERE DATE(sessioncreate.sdate) = %s',(today_date))

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement