I have written this function to format time and date in my Flask app with Babel:
import babel
from flask import Flask
app = Flask(__name__)
def format_datetime(value, format='medium'):
if format == 'full':
format = "EEEE MMMM, d, y 'at' h:mma"
elif format == 'medium':
format = "EE MM, dd, y h:mma"
print(format)
return babel.dates.format_datetime(date, format)
app.jinja_env.filters['datetime'] = format_datetime
strTime = '2021-01-07 12:13:07'
print(format_datetime(strTime))
But when I run this it raises this exception:
Traceback (most recent call last):
File "C:\Users\user\Desktop\timedateTest\timepy.py", line 26, in <module>
print(format_datetime(strTime))
File "C:\Users\user\Desktop\timedateTest\timepy.py", line 18, in format_datetime
return babel.dates.format_datetime(date, format) AttributeError: module 'babel' has no attribute 'dates'
What I am doing wrong?
Try changing:
import babel
to:
from babel.dates import format_date, format_datetime, format_time