Just do "-1" of current date to get previous date or yesterday's date, but it's not so efficient especially if your script runs for long time and every time your script runs if you do "-1" then you are screwed ?? want to know why ?
say today is feb 10th, do "-1" and you'll get 1 as shown below.
As a matter of fact i was expecting 31 (jan 31), If you are in this kind of situation want to go to past days and so certain about month and year, try 'timedelta' from datetime module
I just showed how it works, you can do a lot as you required.
gil ...
$ ipython In [25]: from datetime import date In [26]: d = date.today() In [27]: d Out[27]: datetime.date(2015, 5, 11) In [28]: d.strftime("%d") Out[28]: '11' In [29]: yesterday = int(d.strftime("%d")) - 1 In [30]: yesterday Out[30]: 10 In [31]:To simulate this situation, i'm just subtracting 11 day's from today's date(feb 10th).
$ ipython In [25]: from datetime import date In [26]: d = date.today() In [27]: d Out[27]: datetime.date(2015, 5, 11) In [28]: d.strftime("%d") Out[28]: '11' In [29]: 11dayold = int(d.strftime("%d")) - 10 In [30]: 11dayold Out[30]: 0 In [31]:
As a matter of fact i was expecting 31 (jan 31), If you are in this kind of situation want to go to past days and so certain about month and year, try 'timedelta' from datetime module
$ python In [34]: from datetime import date, timedelta In [35]: d = date.today() In [36]: d Out[36]: datetime.date(2015, 5, 11) In [37]: d - timedelta(days=1) Out[37]: datetime.date(2015, 5, 10) In [38]: yesterday = d - timedelta(days=1) In [39]: yesterday.strftime("%d") Out[39]: '10' In [40]: yesterday = d - timedelta(days=11) In [41]: yesterday Out[41]: datetime.date(2015, 4, 30) In [42]: yesterday.strftime("%d") Out[42]: '30'
I just showed how it works, you can do a lot as you required.
gil ...
0 Comments