How can I get the previous day from a date?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Alex Corvis
Posts: 2
Joined: Thu Jun 23, 2022 9:45 am

How can I get the previous day from a date?

Post by Alex Corvis » Thu Jun 23, 2022 9:55 am

Hi all,

I've the following code which obtains the actual date:

Code: Select all

today=str("%4d-%02d-%02dT%02d:%02d:%02dZ" % utime.localtime()[:6])
Now, I need to get the same format date BUT from PREVIOUS DAY.

Can anyone help me? I've seen that datatime is not available in modules, which contains timedelta method that might help

Many thanks in advance.

Lobo-T
Posts: 36
Joined: Tue Nov 16, 2021 2:36 pm

Re: How can I get the previous day from a date?

Post by Lobo-T » Thu Jun 23, 2022 10:19 am

Code: Select all

time.localtime(time.time()-60*60*24)
Maybe?

Alex Corvis
Posts: 2
Joined: Thu Jun 23, 2022 9:45 am

Re: How can I get the previous day from a date?

Post by Alex Corvis » Thu Jun 23, 2022 1:38 pm

Lobo-T wrote:
Thu Jun 23, 2022 10:19 am

Code: Select all

time.localtime(time.time()-60*60*24)
Maybe?
It's working! Thank you so much! I didn't realise to do like this!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How can I get the previous day from a date?

Post by jimmo » Thu Jun 23, 2022 1:53 pm

Alex Corvis wrote:
Thu Jun 23, 2022 1:38 pm
It's working! Thank you so much! I didn't realise to do like this!
Just be really careful that in general this won't work when timezones are involved -- the same time yesterday is not always exactly 24 hours ago.

Post Reply