Forum Archive

set values for picker.date ?

AddledBadger

Hi,
Searched the docs and forums but can’t seem to find this. Any help on how to set a ui date picker to a particular value, rather than the current time as it’s displaying in my code at the moment. For example, 8am tomorrow.
Thanks, Terry

ramvee

Hope this helps..

# sets date time picker
# you can get details of different parameters from strftime.org
import datetime, ui
date_picker = ui.DatePicker(bg_color=1)
date_picker.mode = ui.DATE_PICKER_MODE_DATE_AND_TIME
date_picker.date = datetime.datetime.strptime('01/03/1958-07:00:PM', '%d/%m/%Y-%I:%M:%p')
date_picker.present()
omz

Here's a variant using parsedatetime for easy-to-read date parsing and math:

import ui
import parsedatetime
cal = parsedatetime.Calendar()
picker = ui.DatePicker(bg_color=1)
picker.date = cal.parseDT('tomorrow 8am')[0]
picker.present('sheet')
coomlata1

Try this.

Phuket2

@AddledBadger , the beta version of Pythonista ships with module called arrow. Really simplifies date and time manipulation. If you are not on the beta version but have installed StaSH, you could pip install arrow yourself. Knowing that when the beta makes it to the App Store, arrow will be included.
arrow docs
StaSH

Look this is not as easy as what the other guys suggest if you dont have arrow installed. I understand why they dont suggest it. Anyway, its another way.

Anyway, the below is using arrow module.

import ui
import arrow

dp = ui.DatePicker(bg_color='white')
dp.mode = ui.DATE_PICKER_MODE_DATE_AND_TIME
arr_date = arrow.get('2017-11-26 08:00:00', 'YYYY-MM-DD HH:mm:ss')
dp.date = arr_date.datetime
dp.present('sheet')