6

I've a small script where I'm getting data from the last few months, based on the timestamp. Right now I'm using current day and a set date (currently May). Here's how I'm defining it:

today_time = int(time.mktime(date.today().timetuple())*1000000)
earlier_time = int(time.mktime(datetime.date(2011,05,01).timetuple())*1000000)

I'd like to change earlier_time from a set date (currently 2011,05,01) to, say, 90 days. I couldn't find how to do this, so your help would be very appreciated.

janecs
  • 199
  • 1
  • 2
  • 9
  • 1
    Subtract 7776000 seconds from the current time? – Wooble Aug 08 '11 at 17:35
  • Kinda related question ("N months ago", as opposed to "N * 30 days ago"): http://stackoverflow.com/questions/6576187/get-year-month-for-the-last-x-months/6576603#6576603 – slowdog Aug 08 '11 at 18:21

1 Answers1

21
import datetime

now = datetime.datetime.now()
then = now - datetime.timedelta(days=90)
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • After the above conversion, In order to convert the type(now) i.e. to 'timestamp', using time.mktime(now.timetuple()) worked just fine for me. – darkdefender27 Nov 16 '17 at 11:46