![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 13
Rep Power: 0
![]() |
I can print 'today', but can I print 'yesterday'?
import time
today = time.strftime("%d %B %Y")
yesterday = str(int(time.strftime("%d"))-1) + time.strftime(" %B %Y")
print today, yesterdayAside: Rather than bumping the other thread again: I got a working MediaWiki bot after finding I was using the wrong version of python with the pywikipediabot. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
I looked into it. I thought there might be a way to set the timezone back 24 hours. Doesn't look like there is.
Maybe you could change your computer clock with the win32api? But that might be very inconvenient. I'd just make a short dictionary of how many days in each month, then a general function around that. Wouldn't be too hard (depending how experienced you are). |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Wee. I decided to try it out myself.
time.gmtime() will convert the seconds since epoch to a date. So if we take the current seconds since epoch time.time() and offset it back a day, voila! import time
def time_zone( offset = 0 ): # DEFAULT = GMT
new = time.gmtime( time.time() + offset*60*60 )
return "Day: %s | Month: %s | Year: %s\n%s:%s:%s"%(new[2],new[1],new[0],new[3],new[4],new[5])
print time_zone( -5 )I'm -5 from GMT, so if I then set it to -29, it gives me yesterday's date. Hope this helps. =D |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
The module datetime is the one you want to use:
import datetime today = datetime.date.today() oneday = datetime.timedelta(days=1) yesterday = today - oneday print today, yesterday
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 13
Rep Power: 0
![]() |
Thanks both, but it's not in the form I'd like, ie '31 January 2006', '1 Febraury 2006'. Is that as simple? I checked some of the documentation on ptyhon.org, but didn't immediately find anything.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
You could do that easily by yourself...
o_O I'll throw something up, just a moment. EDIT: import time
def yday(time_zone): #HOURS FROM GMT!!
new = time.gmtime(time.time()+(time_zone-24)*3600)
mon = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
return "%s %s %s"%(new[2],mon[int(new[1])-1],new[0])
today = time.strftime("%d %B %Y")
yesterday = yday(-5)
print today, yesterdayThen just change -5 to how many hours you are from GMT. |
|
|
|
|
|
#7 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
print today.strftime("%e %B %Y"), yesterday.strftime("%e %B %Y") |
|
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
-T. |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
With Windows use the same code you use for time:
import datetime
today = datetime.date.today()
yesterday = today - datetime.timedelta(days = 1)
print today.strftime("%d %B %Y"), yesterday.strftime("%d %B %Y") # 28 January 2006 27 January 2006
# or military date
print today.strftime("%d%b%Y"), yesterday.strftime("%d%b%Y") # 28Jan2006 27Jan2006
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|