Search code examples
pythoncalendarepoch

How to compare current date with past epoch dates?


I would like a way to obtain the milliseconds epoch time of 14 days from whatever the current date might be, not a difference but the complete string.

Never really worked with the time module and unfamiliar with epoch time so not really sure where to start.

No code at the moment due to logistical things I may not be considering

Ideally, The date that is 14 days in the past would be compared to epoch times in a dictionary and return a result, however I can write the logic for that portion. I am more interested in the logic for getting the proper date in the past.


Solution

  • You can use the datetime module to give you a datetime object that is 14 days ago. The code for that is

    import datetime
    two_weeks_ago = datetime.datetime.now() - datetime.timedelta(days=14)
    

    Once you have a datetime object you can display it in any number of ways. two_weeks_ago.timestamp() while give you the unix-epoch timestamp.