Some Python Time, Datetime Common Usage Example

1. Get Current Time.

>>> import datetime,time
>>> 
>>> now = time.strftime("%Y-%m-%d %H:%M:%S")
>>> 
>>> print(now)
2019-03-29 21:27:20
>>> 
>>> now = datetime.datetime.now()
>>> 
>>> print(now)
2019-03-29 21:27:20.218849

2. Get The Date Of  Last Month’s Last Day.

>>> delta_day = datetime.timedelta(1)
>>> 
>>> this_month_first_day = datetime.date(datetime.date.today().year,datetime.date.today().month,1)
>>> 
>>> this_month_first_day
datetime.date(2019, 3, 1)
>>> 
>>> last = this_month_first_day - delta_day
>>> 
>>> print(last)
2019-02-28

3. Gets The Time Difference (in Seconds).

Time difference is often used to calculate the running time of a program.

>>> start_time = datetime.datetime.now()
>>> 
>>> #long running
... time.sleep(1)
>>> 
>>> end_time = datetime.datetime.now()
>>> 
>>> delta_time = end_time - start_time
>>> 
>>> print(delta_time)
0:00:01.001518
>>> 
>>> print(delta_time.seconds)
1
>>> 
>>> print(delta_time.microseconds)
1518

4. Calculate The Time 2 Hours Backward From Current Time.

>>> d1 = datetime.datetime.now()
>>> 
>>> d1.ctime()
'Fri Mar 29 21:58:25 2019'
>>> 
>>> d2 = d1 + datetime.timedelta(hours=2)
>>> 
>>> d2.ctime()
'Fri Mar 29 23:58:25 2019'
>>> 

5. Get Today, Yesterday and Tomorrow.

>>> today=datetime.date.today()   
>>> 
>>> today
datetime.date(2019, 3, 29)
>>> 
>>> oneday=datetime.timedelta(days=1)   
>>> 
>>> yesterday=today - oneday
>>> 
>>> yesterday
datetime.date(2019, 3, 28)
>>> 
>>> tomorrow=today + oneday
>>> 
>>> tomorrow
datetime.date(2019, 3, 30)

6. Get The Date Of A Few Previous Days Of A Given Time In A List.

>>> today=datetime.date.today() 
>>> 
>>> today
datetime.date(2019, 3, 29)
>>> 
>>> oneday=datetime.timedelta(days=1)  
>>> 
>>> li=[] 
>>> 
>>> for i in range(0,3):
...     today=today-oneday
...     today_str = str(today)[0:10]
...     li.append(today_str)
... 
>>> li
['2019-03-28', '2019-03-27', '2019-03-26']
>>>

7. Converts A String To Datetime Type.

>>> datetime.datetime.strptime('2019-03-29','%Y-%m-%d') 
datetime.datetime(2019, 3, 29, 0, 0)

8. Get Year, Month, Day In Time String.

>>> now = datetime.datetime.now()
>>> 
>>> now
datetime.datetime(2019, 3, 29, 22, 15, 6, 98877)
>>> 
>>> str(now)[0:10]
'2019-03-29'

9. Calculate How Many Days Are The Two Dates Apart.

>>> begin_date_str = '2018-03-1'
>>> 
>>> end_date_str = '2019-01-03'
>>> 
>>> format="%Y-%m-%d";   
>>> 
>>> begin_date = datetime.datetime.strptime(begin_date_str,format)
>>> 
>>> end_date = datetime.datetime.strptime(end_date_str,format)
>>> 
>>> one_day=datetime.timedelta(days=1)   
>>> 
>>> count=0  
>>> 
>>> while begin_date != end_date: 
...     end_date = end_date - one_day
...     count+=1  
... 
>>> count
308

10. Get All Date String In A List Between Begin And End Date.

>>> begin_date_str = '2019-01-1'
>>> 
>>> end_date_str = '2019-01-03'
>>> 
>>> format="%Y-%m-%d";   
>>> 
>>> begin_date = datetime.datetime.strptime(begin_date_str,format)
>>> 
>>> end_date = datetime.datetime.strptime(end_date_str,format)
>>> 
>>> one_day=datetime.timedelta(days=1)   
>>> 
>>> li = []
>>> 
>>> while begin_date <= end_date:  
...     li.append(str(begin_date)[0:10])
...     begin_date = begin_date + one_day 
... 
>>> li
['2019-01-01', '2019-01-02', '2019-01-03']
>>>

11. Get Year Of Current Time.

>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2019, 3, 29, 22, 56, 21, 760158)
>>> dt.year
2019

12. Get Month Of Current Time.

>>> dt = datetime.datetime.now()
>>> dt
datetime.datetime(2019, 3, 29, 22, 56, 21, 760158)
>>> dt.month
3