I need to display the date and time on the browser through a variable.I am just beginner of python and flask. Here is my code. Please suggest an idea or two to point me in the right direction.
from datetime import datetime
date = datetime.now()
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return date
if __name__ == "__main__":
app.run(debug=True)
But it shows "internal server error"
You must put the date declaration inside your route:
from datetime import datetime
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
date = datetime.now()
return str(date)
if __name__ == "__main__":
app.run(debug=True)
If you want the date in a more presentative format replace:
return str(date)
by:
return date.strftime("%d/%m/%y")
as others suggested in the comments
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