✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Why does date range picker return values with time for first time and only dates after that?

dash_core_components.DatePickerRange first returns dates with time when selected dates for the first time and only dates from second time onwards.

In below example, I am printing dates as i select dates from picker.
when i select nothing:
image
when i choose first date:
image
after i choose second date:
image
and this is the way it returns after this.

Here’s my code:

from dash import Dash
import dash_core_components as dash_core
import dash_html_components as dash_html
from dash.dependencies import Input, Output
from datetime import datetime


check_app = Dash(__name__)

check_app.layout = dash_html.Div(
    children=[
        dash_core.DatePickerRange(id='check_date_picker',
                                  start_date=datetime(2014, 1, 1),
                                  end_date=datetime(2015, 1, 31)),
        dash_html.Br(),dash_html.Br(),
        dash_html.H6(id='printing_dates')
    ]
)


@check_app.callback(Output(component_id='printing_dates', component_property='children'),
                    [Input(component_id='check_date_picker', component_property='start_date'),
                     Input(component_id='check_date_picker', component_property='end_date')])
def print_selected_dates(start_date, end_date):
    return str((start_date, end_date))


if __name__ == "__main__":
    check_app.run_server()

How do i have dash always return only the dates?

Hi Naveen,

When you create your datetime object start_date=datetime(2014, 1, 1), even though you aren’t specifying a time, the object will still have hour, minute, and second all equal to 0.

The datetime.date() function will strip the time leaving you with just date, so you can simply define your dates with start_date=datetime(2014, 1, 1).date().

Hope this helps!

1 Like