Introduction

This package is meant to supply widgets for picking a time and a datetime, using the default HTML5 input controls. For a date-onpy input, use the one in ipywidgets core (ipywidgets.DatePicker).

The time picker

To create a time picker, use the TimerPicker widget from ipydatetime:

[1]:
import ipydatetime
import ipywidgets
import datetime
[2]:
time_picker = ipydatetime.TimePicker()
[3]:
time_picker
[3]:

Here is a label showing how the kernel will format this value. Note that if the locale setting of the browser and kernel are different, the formatting of the time might also differ:

[4]:
time_lbl = ipywidgets.Label()

def update_time(change=None):
    time_lbl.value = str(time_picker.value)

update_time()
time_picker.observe(update_time, names='value')

time_lbl
[4]:

By default, the browser only shows hours/minutes. To input seconds and optionally milliseconds, either give an initial value with non-zero seconds (milliseconds), or specify a smaller step attribute.

[5]:
ipydatetime.TimePicker(value=datetime.time(23, 15, 32), step=1)
[5]:
[6]:
ipydatetime.TimePicker(value=datetime.time(23, 15, 32, 7000), step=0.001)
[6]:
[7]:
ipydatetime.TimePicker(value=datetime.time(23, 15), step=5)
[7]:
[8]:
ipydatetime.TimePicker(value=datetime.time(23, 15), step=0.1)
[8]:

The datetime picker

To create a datetime picker, use the DatetimePicker widget from ipydatetime:

[9]:
datetime_picker = ipydatetime.DatetimePicker()
[10]:
datetime_picker
[10]:

Here is a label showing how the kernel will format this value. As for the TimePicker, if the locale setting of the browser and kernel are different, the formatting of the datetime might also differ.

[11]:
datetime_lbl = ipywidgets.Label()

def update_datetime(change=None):
    if datetime_picker.value is None:
        datetime_lbl.value = 'None'
    else:
        # Present it using kernel system timezone:
        datetime_lbl.value = str(datetime_picker.value)

update_datetime()
datetime_picker.observe(update_datetime, names='value')

datetime_lbl
[11]:

Time zones

There are two points worth to note with regards to timezones: - The browser always picks datetimes using its timezone. - The kernel always gets the datetimes in the default system timezone of the kernel (see https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone with None as the argument).

This means that if the kernel and browser have different timezones, the default formatting of the timezones might differ (though they will represent the same point in time).

[12]:
import pytz
ipydatetime.DatetimePicker(value=datetime.datetime(
    2000, 1, 1, 0, 0, tzinfo=pytz.timezone('Australia/Sydney')))
[12]:

Naive picker

In some cases you might want to be able to pick naive datetime objects, i.e. timezone-unaware datetimes. To quote the Python 3 docs:

Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

[13]:
naive_picker = ipydatetime.NaiveDatetimePicker()
[14]:
naive_picker
[14]:
[15]:
naive_lbl = ipywidgets.Label()

def update_naive(change=None):
    naive_lbl.value = str(naive_picker.value)

update_naive()
naive_picker.observe(update_naive, names='value')

naive_lbl
[15]:
[ ]: