A problem with Time

S

special_dragonfly

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
 
S

Shawn Milochik

import time


oneDay = 60 * 60 * 24 #seconds in one day

date = time.time()

yesterday = date - oneDay
 
D

Diez B. Roggisch

special_dragonfly said:
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
 
G

Gary Herron

special_dragonfly said:
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:

15082007

Gary Herron
 
M

MRAB

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))
 
R

Roger Miller

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))

This is something I have wondered about. The C library mktime
function is
documented to fix up out of range values,. For example July 32
becomes
August 1 and August -1 becomes July 31. Python presumably inherits
this
very useful (and seemingly not well known) behavior, but it is not
documented. Is this just an oversight, or is it intentional on the
grounds
that it might be platform-dependent? Any language lawyers out there
that
would care to comment?
 
D

Dick Moores

At said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top