2017年10月9日 星期一

[ Python 常見問題 ] How to get the date N days ago in Python

Source From Here 
Below will use datetime & timedelta to get the date N days ago: 
>>> from datetime import datetime, timedelta 
>>> N = 2 // Calculate 2 days ago from now on 
>>> date_N_days_ago = datetime.now() - timedelta(days=N) 
>>> print datetime.now() 
2017-10-10 12:22:18.817720 
>>> print date_N_days_ago 
2017-10-08 12:22:07.384718


沒有留言:

張貼留言

[ Py DS ] Ch1 - IPython: Beyond Normal Python

Source From  Here   Keyboard Shortcuts in the IPython Shell   If you spend any amount of time on the computer, you’ve probably found a u...