473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1150
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Phil Grimpo | last post by:
I had previously explained this problem in a different thread, but now that I have an IISState log, I figured I'd re-start the thred. My situation and the log are following... I have a very odd...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
0
by: Palle Girgensohn | last post by:
Hi! A performance question: I have some tables: Tabell "public.person" Kolumn | Typ | Modifierare ------------------+--------------------------+---------------...
0
by: Norm Wong | last post by:
If anyone is interested in using db2uext2 with Cygwin gcc compiler on Windows, I've modified the IBM provided sample with the attached file. There are two main modifications. The mkdir command...
6
by: Michael Bulatovich | last post by:
I have a very simple db I use for keeping track of hours, tasks, projects, clients etc. It has a form that I use to enter data. Currently the form has a textbox for a field called "start time",...
13
by: Stuart Bishop | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi. I'm trying to determine the best way of saying 'The current time in UTC with no time zone information'. I'm currently using...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
17
by: Timothy.Rybak | last post by:
Hello all, This is my first attempt at an application, so kid gloves are appreciated. I need to make a very simple form that only has a few elements. One is TraceCode - a text field that is...
2
by: Prophet | last post by:
Can someone help me troubleshoot this IISSTATE log ? I have tried to make sense out of it but with no luck I am currently running IIS5 on a Win 2k box. Hosting 3 web sites and runnning asp...
6
by: lukasso | last post by:
Hi, this is my code that should produce something like a timetable for a few days with each day divided into 30 minute pieces. It makes query from MySQL and then creates a 2d $array which then is to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.