473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(y esterdaytime,'% j')
print newtime
(1900, 1, 22, 0, 0, 0, 0, 22, -1)

can someone enlighten me please?

Thanks
David

Jul 18 '05 #1
4 4547
On Thu, 22 Jan 2004, ne************* **********@quan ta...dspambo t.com wrote:
I am trying to find a simple way to get "yesterday' s" date.

import datetime
today = datetime.date.t oday()
today datetime.date(2 004, 1, 23) yesterday = today - datetime.timede lta( 1 )
yesterday

datetime.date(2 004, 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************* **********@quan ta...dspambo t.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(tim e.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************* **********@quan ta...dspambo t.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)).d ay

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************* **********@quan ta...dspambo t.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)).d ay

22


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

Thanks a lot,
David
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3074
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
5
7923
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August 2005", (or a timestamp that could be formatted). I have been thinking about creating a loop that increases the source date $myDate with 24*60*60 until date("l", $myDate) = "Monday" but maybe I am trying too complicated solutions and overseeing...
5
724
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash (DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name date) <asp:DataGrid id="RssNewsGrid"
12
13885
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL does not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
2
4100
by: Florian G. Pflug | last post by:
Hi Since sometime yesterday, my postgresql (7.4.5) reports "ERROR: cannot compare arrays of different element types", when I analyze a specific table in my database. Here is the tables definition: Column | Type | Modifiers -------------------+------------------------+---------------------------------------------------- self | datagraph."GOLink" | not null default
23
3192
by: Phil Powell | last post by:
// OBTAINED FROM http://www.javascripter.net/faq/settinga.htm // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY AND THUS EXPIRE function setCookie(name, value, days, docObj) { var today = new Date(); var expire = new Date(); if (days == null || isNaN(days) || days == 0) days = 1; if (days >= 1 || days < 0) expire.setTime(today.getTime() + 3600000 * 24 * days);
0
3047
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
0
2622
by: Just_a_fan | last post by:
Some folks have searched, from time to time, for a dual axis MSChart with different scales on the two Y axes. The sample, extracted from running code I wrote, produces a graph with MSChart (VB9) which shows a time axis on X and two somewhat different value ranges on Y and Y2. This code produces two axes with positive and negative values, if called for, and also ranges the Y and Y2 values to match the data and still remain readable. ...
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.