Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: parsing DateTimeField input with a function #777

Closed
rickwierenga opened this issue Mar 6, 2023 · 1 comment
Closed

Comments

@rickwierenga
Copy link

Currently, the DateTimeField takes a format parameter:

DateTimeField("datetime", format='%Y-%m-%d %H:%M:%S')

It would be great to add an optional parse parameter, which would take a function converting a string into a datetime object. This would make the field more extendible. Examples:

DateTimeField("datetime", parse=datetime.datetime.fromisoformat)
DateTimeField("datetime", parse=lambda x: datetime.datetime.fromtimestamp(int(x))

This option would be mutually exclusive with the format parameter.

Idem for TimeField and DateField.

Let me know if I can create a PR for this.

@azmeuk
Copy link
Member

azmeuk commented Jul 21, 2023

Hi. You can actually already do this with filters:

>>> import wtforms
>>> import datetime
>>> class Form(wtforms.Form):
...     dt = wtforms.DateTimeField(filters=[datetime.datetime.fromisoformat])
>>> f = Form(dt='2023-07-21T18:11:32.295417')
>>> f.dt.data
datetime.datetime(2023, 7, 21, 18, 11, 32, 295417)
>>> class Form(wtforms.Form):
...     dt = wtforms.DateTimeField(filters=[datetime.datetime.fromtimestamp])
>>> f = Form(dt=1689955998.857561)
>>> f.dt.data
datetime.datetime(2023, 7, 21, 18, 13, 18, 857561)

Related to #793

@azmeuk azmeuk closed this as completed Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants