468,544 Members | 1,858 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,544 developers. It's quick & easy.

get "yesterday's" date in iso format

Hi,

I am trying to find a simple way to get "yesterday's" date. I know I can
build various check to seee if we are in leap year, if we are the 1st then
yesterday is the 31(or 30 depending of the month) etc. but there is got to
be a simpler way than that. I was thinking of using the gmtime (field 7 as
you can see int the code)then substract one, then use strptime to rebuild
the time but it doesn't work(probably because I misunderstood strptime):

improt time
gmt = time.gmtime()
yesterdaytime = str(int(gmt[7]) - 1)
print gmt[7]
print yesterdaytime
newtime = time.strptime(yesterdaytime,'%j')
print newtime
(1900, 1, 22, 0, 0, 0, 0, 22, -1)

can someone enlighten me please?

Thanks
David

Jul 18 '05 #1
4 4155
On Thu, 22 Jan 2004, ne***********************@quanta1.wo...pidspambot.com wrote:
I am trying to find a simple way to get "yesterday's" date.

import datetime
today = datetime.date.today()
today datetime.date(2004, 1, 23) yesterday = today - datetime.timedelta( 1 )
yesterday

datetime.date(2004, 1, 22)

--
\ "We must become the change we want to see." -- Mahatma Gandhi |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2
ne***********************@quanta1.wo...pidspambot.com writes:

I am trying to find a simple way to get "yesterday's" date. I know I can
build various check to seee if we are in leap year, if we are the 1st then
yesterday is the 31(or 30 depending of the month) etc. but there is got to
be a simpler way than that.


Since time.time() gives the time in seconds since the epoch, you could
just subtract a day from that to get yesterday.
import time
time.gmtime(time.time()-24*60*60)

(2004, 1, 22, 4, 36, 3, 3, 22, 0)

I hope this helps,

Tim
Jul 18 '05 #3
Tim Heaney wrote:
ne***********************@quanta1.wo...pidspambot.com writes:

I am trying to find a simple way to get "yesterday's" date.


Since time.time() gives the time in seconds since the epoch, you could
just subtract a day from that to get yesterday.


Even better:
from datetime import date, timedelta
(date.today() - timedelta(1)).day

22

yours,
Gerrit.

--
179. If a "sister of a god," or a prostitute, receive a gift from her
father, and a deed in which it has been explicitly stated that she may
dispose of it as she pleases, and give her complete disposition thereof:
if then her father die, then she may leave her property to whomsoever she
pleases. Her brothers can raise no claim thereto.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #4
On Fri, 23 Jan 2004 17:54:15 +0100, Gerrit Holl wrote:
Tim Heaney wrote:
ne***********************@quanta1.wo...pidspambot.com writes:
>
> I am trying to find a simple way to get "yesterday's" date.


Since time.time() gives the time in seconds since the epoch, you could
just subtract a day from that to get yesterday.


Even better:
from datetime import date, timedelta
(date.today() - timedelta(1)).day

22


Excellent. I guess I skipped the timedela section of the datetime module.

Thanks a lot,
David
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Don Vaillancourt | last post: by
5 posts views Thread by Martien van Wanrooij | last post: by
23 posts views Thread by Phil Powell | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.