Dat_input stores function instead of value into session state


date_input widget stores the datetime function into session state instead of value

Steps to reproduce

Code snippet:

with st.form("add_user", clear_on_submit=False):
  firstName = st.text_input("Vorname", key='firstName')
  lastName = st.text_input("Nachname", key='lastName')

  birthDate = st.date_input("Birthday", min_value=datetime.date(1910, 2, 1), format='DD/MM/YYYY', key='birthDate')
 submitted = st.form_submit_button("Save")
 if submitted:
   def add_user():

new_user = { 'firstName' : st.session_state['firstName'],
                 'lastName' : st.session_state['lastName'],
                 'birthDate' : str(st.session_state['birthDate']),
                 'entryDate' : st.session_state['entryDate'],
  return new_user


“firstName”:“some first name”
“lastName”: “some last name”
“entryDate”:“datetime.date(2023, 9, 22)”

Expected behavior:

Value is stored in the session state - i.e. 2023-09-22 or document the representation of the data

Actual behavior:

Datetime function as string

Hi @mauermbq ,

You can use iso format for that. The input_date is returning a string but your entry date update is returning the date function. See my example,

from datetime import date, datetime

with st.form("add_user", clear_on_submit=False):
    firstName = st.text_input("Vorname", key="firstName")
    lastName = st.text_input("Nachname", key="lastName")

    birthDate = st.date_input(
        min_value=date(1910, 2, 1),
        # format="DD/MM/YYYY",

    submitted = st.form_submit_button("Save")
    if submitted:

        def add_user():
            new_user = {
                "firstName": st.session_state["firstName"],
                "lastName": st.session_state["lastName"],
                "birthDate": str(st.session_state["birthDate"]),
                "entryDate": datetime.now().date().isoformat(),
            return new_user


After fixing the indentation, clicking Save raises a KeyError. Looks like you forgot to post the code where you assign a value to st.session_state["entryDate"].

…sorry this was copy/paste error, my original code is more complex.

However, that st.write renders correct and that extracting the same session object to a dict returns something completely different was confusing. It would be helpful in the doc explaining what python object is returning exactly, and what happens under the hood during rendering.

birthDate = st.date_input(“Birthday”, min_value=datetime.date(1910, 2, 1), format=‘DD/MM/YYYY’, key=‘birthDate’)

when entering 02/10/1987 you get:

st.write(st.session_state[‘birthDate’]) → Output for example 02-10-1987 (as expected)

new_user = { ‘birthDate’: st.session_state[‘birthDate’])} → {‘birthDate’: “datetime.date(1987, 10, 02)”}
(that’s really unexpected)

That is because st.write actually calls st.json when passed a dictionary, then st.json serializes its argument as JSON. Instances of datetime.date are serialized to their string representation using repr (not str), which is what you are seeing.