# Validate datetime in Django

**URL:** <https://forum.djangoproject.com/t/validate-datetime-in-django/7782>\
**Category:** Using Django\
**Created:** [May 3, 2021, 4:56pm UTC](https://forum.djangoproject.com/t/validate-datetime-in-django/7782 "2021-05-03T16:56:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![piwko7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/piwko7/32/4420_2.png) [@piwko7](https://forum.djangoproject.com/u/piwko7)\
**Post date:** [May 3, 2021, 4:56pm UTC](https://forum.djangoproject.com/t/validate-datetime-in-django/7782/1 "2021-05-03T16:56:29Z")

</div>

Hi,  
I have form with one datetime field.  
How can i validate that date is valid (for example doesn’t have any letters) ?  
I was trying do that by clean function in forms.py:  
valid\_date = isinstance(duel\_date, datetime.datetime)  
But is not working

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [May 3, 2021, 5:17pm UTC](https://forum.djangoproject.com/t/validate-datetime-in-django/7782/2 "2021-05-03T17:17:19Z")

</div>

Hi!

What’s not working? Do you have any error message? It’s not validating anything?

If you want to validate only one field, remember you need to create a method named `clean_<your_field_name>` inside your form. [Validating a specific field](https://docs.djangoproject.com/en/3.2/ref/forms/validation/#cleaning-a-specific-field-attribute)

Also, if you’re using HTML templates, it’s good to define the field input type to leverage browser validations.  
Take a look at this example of a [datetime-local input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local) (At the moment it only works with Chrome, Edge)

---

<div class="post-metadata">

**Author:** ![piwko7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/piwko7/32/4420_2.png) [@piwko7](https://forum.djangoproject.com/u/piwko7)\
**Post date:** [May 3, 2021, 8:26pm UTC](https://forum.djangoproject.com/t/validate-datetime-in-django/7782/3 "2021-05-03T20:26:49Z")

</div>

I got that problem, if i put i date field for example:  
2021-05-03 22:18:10 abc  
Then clean\_\<your\_field\_name\> is not proceed becouse form is invalid (invalid input).

---

<div class="post-metadata">

**Author:** ![marcorichetta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcorichetta/32/26473_2.png) [@marcorichetta](https://forum.djangoproject.com/u/marcorichetta)\
**Post date:** [May 4, 2021, 12:44pm UTC](https://forum.djangoproject.com/t/validate-datetime-in-django/7782/4 "2021-05-04T12:44:11Z")

</div>

Ok. Did you check the [form errors](https://docs.djangoproject.com/en/3.1/ref/forms/api/#django.forms.Form.errors) to see what’s making it invalid?
