472,133 Members | 997 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

A problem with Time

Hello,

I need to return the date yesterday in the form DDMMYYYY. I looked through
the modules: time, datetime and calendar but can't find anything that leaps
out at me.

The problem I'm having is that although I can use time.localtime and get a
tuple of the year, month, day and so forth, I don't believe I can just minus
1 from the day, because I don't think it's cyclic, also, I can't see the
date being linked in with the month.

So is there any way of getting yesterdays date?

Thank You

Dominic
Aug 16 '07 #1
7 1104
import time
oneDay = 60 * 60 * 24 #seconds in one day

date = time.time()

yesterday = date - oneDay
Aug 16 '07 #2
special_dragonfly schrieb:
Hello,

I need to return the date yesterday in the form DDMMYYYY. I looked through
the modules: time, datetime and calendar but can't find anything that leaps
out at me.

The problem I'm having is that although I can use time.localtime and get a
tuple of the year, month, day and so forth, I don't believe I can just minus
1 from the day, because I don't think it's cyclic, also, I can't see the
date being linked in with the month.

So is there any way of getting yesterdays date?
RTFM is the answer...

import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
print yesterday
Diez
Aug 16 '07 #3
On 2007-08-16, Shawn Milochik <Sh***@Milochik.comwrote:
import time
oneDay = 60 * 60 * 24 #seconds in one day

date = time.time()

yesterday = date - oneDay
Or use a timedelta.
>>import datetime
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
yesterday.strftime('%m%d%Y')
'08152007'

--
Neil Cerutti
It might take a season, it might take half a season, it might take a year.
--Elgin Baylor
Aug 16 '07 #4
special_dragonfly wrote:
Hello,

I need to return the date yesterday in the form DDMMYYYY. I looked through
the modules: time, datetime and calendar but can't find anything that leaps
out at me.

The problem I'm having is that although I can use time.localtime and get a
tuple of the year, month, day and so forth, I don't believe I can just minus
1 from the day, because I don't think it's cyclic, also, I can't see the
date being linked in with the month.

So is there any way of getting yesterdays date?

Thank You

Dominic
Here's how I'd do it:

>>import time
secondsPerDay = 24*60*60
today = time.time()
yesterday = today - secondsPerDay
print time.strftime("%d%m%Y",time.localtime(today))
16082007
>>print time.strftime("%d%m%Y",time.localtime(yesterday))
15082007

Gary Herron
Aug 16 '07 #5
On Aug 16, 10:54 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
RTFM is the answer...
I don't know. I remember scratching my head for a day or two over the
module explanations and instructions until I found a little howto with
a lot of explicite examples.

http://pleac.sourceforge.net/pleac_p...sandtimes.html

rd

Aug 16 '07 #6
On Aug 16, 4:30 pm, "special_dragonfly" <Domi...@PLEASEASK.co.uk>
wrote:
Hello,

I need to return the date yesterday in the form DDMMYYYY. I looked through
the modules: time, datetime and calendar but can't find anything that leaps
out at me.

The problem I'm having is that although I can use time.localtime and get a
tuple of the year, month, day and so forth, I don't believe I can just minus
1 from the day, because I don't think it's cyclic, also, I can't see the
date being linked in with the month.

So is there any way of getting yesterdays date?
As well as the other replies, this also works (as far as I can tell!):

import time
today = time.localtime()
yesterday = today[ : 2] + (today[2] - 1, ) + today[3 : ]
yesterday = time.localtime(time.mktime(yesterday))

Aug 16 '07 #7
At 08:30 AM 8/16/2007, special_dragonfly wrote:
>Hello,

I need to return the date yesterday in the form DDMMYYYY. I looked through
the modules: time, datetime and calendar but can't find anything that leaps
out at me.

The problem I'm having is that although I can use time.localtime and get a
tuple of the year, month, day and so forth, I don't believe I can just minus
1 from the day, because I don't think it's cyclic, also, I can't see the
date being linked in with the month.

So is there any way of getting yesterdays date?
The question has already been well-answered, but since I've found
using the datetime module to be tough going, I was wondering if
either of these would be easier to understand and use:
1. <http://www.egenix.com/products/python/mxBase/mxDateTime/>
I see that mxDateTime comes with a 55-page manual as a PDF.

2. <http://labix.org/python-dateutil>

Dick Moores


Aug 19 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Palle Girgensohn | last post: by
reply views Thread by Norm Wong | last post: by
6 posts views Thread by Michael Bulatovich | last post: by
13 posts views Thread by Stuart Bishop | last post: by
3 posts views Thread by cj | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.