Setting DatePicker to datetime.datetime.now()

Hi all,

First post…be gentle. I have searched the forums for an answer but haven’t found anything yet.

I’ve put together an app for myself to monitor temperature and humidity in my greenhouse (everyone’s gotta have a hobby, right?). Eventually, this’ll be logged automatically with a Rasp Pi and Uplink. But for now, I’ve put together a simple app fo rme to enter values manually and save them to a data table. Easy enough.

To speed things up, rather than have to pick time and date on the DatePicker every time I want to enter a reading, I’ve put a “Now” button next to the DatePicker which uses the following to set the date and time to now:

def button_now_click(self, **event_args):
    self.today = datetime.datetime.now()
    self.date_and_time.date = "%d:%d %d %s %d" % (self.today.hour, self.today.minute, self.today.day, self.today.strftime("%b"), self.today.year)
    pass

The button works, filling in the time and date. However, the time behaviour is not as expected. I’ve just tried it (at 11:11) and the time entered to the DatePicker was 11:04. And it always seems to choose hh:04, no matter what time of day I try it. Previously, I was entering the time and date now as default text and the behaviour was the same.

Any ideas on why this is behaving like this?

Also, as a slight aside, is there a way to clear the currently set date and re-display placeholder text in a DatePicker? Apologies for the 2 questions in one…

Thanks in advance
jon

1 Like

hi @guitarmanjon and welcome to the forum,

You can use the datepicker format option to display the date as you want.

https://anvil.works/docs/api/anvil#DatePicker_attributes

so in properties set the format property as: %H:%M %d %b %Y

or in code…

from datetime import datetime 
self.datepicker.format = '%H:%M %d %b %Y'

def button_now_click(self, **event_args):
  self.datepicker.date = datetime.now()

similarly if you want to set the text property of a label say just do

self.label.text = datepicker.date.strftime('%H:%M %d %b %Y')

rather than using %d:%d

sidenote: anvil supports fstrings which can make the use of %d symbol redundant in most cases

3 Likes

Thanks @stucork.

That is a much more elegant way of getting current time and date, and formatting the DatePicker. I hadn’t thought of that. And it works nicely.

What is interesting (to me at least) is that the method you show above uses the same Python library (datetime) to get the current time and date, but what is displayed is the actual current time. Whereas my implementation always had hh:04.

Anyway, it’s fixed. Many thanks.
jon

Something to do with it being April?

happy to take a look at the code if you have a clone link.


p.s. sounds like a cool project - be sure to share it on Show and Tell when you have a prototype!

Yeah, maybe. But I thought that pulling

self.today.minute()

should always return the minute?
Maybe I’ll try it next month and see if I start getting hh:05 !

I have a prototype. But at the moment anyone can enter a reading. I’d like to lock it down a little before letting everyone put in all sorts of data. My tomatoes won’t grow in temperatures of 300 degrees! I’m going to add a plot to show historical data, and maybe add a login for me to record real data (with a usable non-logged-in version).

I’m using this as a good excuse to learn more Python and Anvil! And to make a digital twin of my greenhouse…

jon

you can share the code without sharing the live app.

This way I can clone it and dig around and see what the weird 04 error was.
Go to the gear :gear: icon and hit share. Then find the clone link to share the source code and data tables.

of course no need if you’d prefer not to share it.

Here you go. Enjoy!

https://anvil.works/build#clone:JY2F4XRTDGAS247V=XNEOE7TZ2W6SGRDGRAI7SM4I

Edit: have reverted back to a version with the strange hh:04 behaviour.

looks like you’d already set the datepicker.format to HH:MM DD MMM YYYY
rather than HH:mm DD MMM YYYY

:rofl:
Oh wow, that’s embarassing. Thanks for picking it up.

1 Like