Hi, I’m very new at Python and Anvil so excuse me in advance.
Is there a way to truncate the datetime output that appears in a data table? I am pulling in datetime for any new entry that a user adds to my database, and then showing it in a datatable in my app. Currently the datetime output shows seconds, miliseconds and timezones.
I would like to shorten the time to just appear as hours, minutes, seconds on the client side output.
I tried to convert the datetime to a strftime string where I could show the time truncated as (%A %H:%M) after reading about that method online, but this didn’t work, but perhaps I did it wrong.
Thank you very much for your reply. I took another look at it and I had not set the column that the strftime was pulling into to a “simple object” type column in the data table which was producing the error. Silly me.
Converting datetime to a strftime object with this code below and changing the column type worked. Thanks for your reply and for the welcome to Anvil and will make sure to post the code that doesn’t work in subsequent questions. Cheers!
now = datetime.now()
local_time = now.strftime(“%A %H:%M”)
(used local_time as an object to add to the datatable)
We do like to try to guide people to finding the solutions themselves rather than just giving a solution. It helps their understanding and encourages good question asking.
Most people here are volunteers, and you’ll find that the less work they have to do to see your actual problem, the more likely they are to post helpful answers. They’re a clever lot
A simpleObject column can certainly hold a string. A text column can, too.
If you ever need to use this column value as a date or datetime, in calculations, that’s going to require more work, to convert it to a useful value. In that case, it may be better to use a column of type date or datetime, which will preserve all the details, and limit the reformatting to the content of your displayed Label.