1

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
TheTruth
  • 37
  • 7

3 Answers3

5

Try changing:

import babel

to:

from babel.dates import format_date, format_datetime, format_time
John Gardounis
  • 125
  • 1
  • 12
  • received this exception AttributeError: 'NoneType' object has no attribute 'days' – TheTruth Jan 07 '21 at 12:06
  • 1
    Or simply `from babel.dates import format_datetime`, as the other two functions are not used. – mkrieger1 Jan 07 '21 at 12:06
  • @TheTruth If the problem related to importing the function is solved, and now there is another problem, please ask a new question (after researching the new problem yourself first). – mkrieger1 Jan 07 '21 at 12:15
  • @TheTruth I think this is a problem related to the default locale you set. Check out this question https://stackoverflow.com/q/4838056/10895906 which is similar to yours – John Gardounis Jan 07 '21 at 12:19
2

You can make use of the datetime module in python.

import datetime

def format_datetime(value, format='medium'):

    # Create a python date object to work on
    date_obj = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
    if format == 'full':
        return date_obj.strftime("%m/%d/%Y, %H:%M:%S")
    elif format == 'medium':
        return date_obj.strftime("%d-%b-%Y, %H:%M:%S")

strTime = '2021-01-07 12:13:07'

Returned formats

# output for format full
'07-January-2021, 12:13:07'

# output for format medium
'07-Jan-2021, 12:13:07'

You can plug any kind of format using a string as a parameter for date_obj.strftime() method.

Date formats can be found here for reference.

Amit Pathak
  • 1,145
  • 11
  • 26
  • running your code in an empty neew file shows error `AttributeError: type object 'datetime.datetime' has no attribute 'datetime'` – TheTruth Jan 07 '21 at 11:58
1

After reading the documentatation at http://babel.pocoo.org/en/latest/api/dates.html I suspect that passing a string into babel.dates.format_datetime is not allowed as this function expects a datetime:

babel.dates.format_datetime(datetime=None, format='medium', tzinfo=None, locale=default_locale('LC_TIME'))
  • datetime – the datetime object; if None, the current date and time is used
  • format – one of “full”, “long”, “medium”, or “short”, or a custom date/time pattern
  • tzinfo – the timezone to apply to the time for display
  • locale – a Locale object or a locale identifier

Also as John Gardounis mentions using a direct reference might help. As you define your own format_datetime function for yourself my suggestion is:

from babel.dates import format_datetime as babel_format_datetime

Then replace this line in your function:

return babel.dates.format_datetime(date, format)

with:

return babel_format_datetime(date, format)
Knut Boehnert
  • 488
  • 5
  • 10