Time, Date, Timestamp in Python: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 65: Line 65:
==Format==
==Format==


===Parsing from String===
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
from datetime import datetime
from datetime import datetime
Line 77: Line 78:


Also see [[#Time.2C_Date_and_Timestamp_Parsing_with_dateutil|Time, Date and Timestamp Parsing with <tt>dateutil</tt>]] below.
Also see [[#Time.2C_Date_and_Timestamp_Parsing_with_dateutil|Time, Date and Timestamp Parsing with <tt>dateutil</tt>]] below.
===Formating as String===
<syntaxhighlight lang='py'>
d = ...
d.strftime("%Y-%m-%d %H:%M")
</syntaxhighlight>


=<SPAN ID='dateutil'></SPAN><tt>dateutil</tt> Module=
=<SPAN ID='dateutil'></SPAN><tt>dateutil</tt> Module=

Revision as of 20:20, 16 May 2024

External

Internal

Overview

The Standard Library datetime module provides datetime, date and time types. The datetime type combines information stored in data and time and it is the most commonly used. Additionally, the dateutil module provides useful extensions.

datetime Module

https://docs.python.org/3/library/datetime.html#module-datetime
https://geekflare.com/calculate-time-difference-in-python/

datetime Overview

The datetime module provides classes for manipulating dates and times. The datetime package documentation seems to recommend the dateutil for time zone support and parsing.

The datetime.datetime Type

Time Interval with timedelta

from datetime import datetime
dt1 = datetime(2022,3,27,13,10,45,46000) 
dt2 = datetime(2022,6,30,14,28) 
tdelta = dt2 - dt1 
print(tdelta) 
print(type(tdelta)) 
print(dt1.day) # prints 27
print(dt1.minute) # prints 10

To get the total number of seconds in timedelta, use total_seconds():

from datetime import datetime
t0 = datetime.now()
...
t1 = datetime.now()
print((t1 - t0).total_seconds())

now Time

from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)

Current Date

With now():

from datetime import datetime
d = datetime.now().strftime("%Y-%m-%d")
print(d) # displays YYYY-mm-dd

With date.today():

from datetime import date
print(str(date.today())) # displays YYYY-mm-dd

Format

Parsing from String

from datetime import datetime
s = '12/31/2023'
d = datetime.strptime(s, "%m/%d/%Y")
assert d.year == 2023
assert d.month == 12
assert d.day == 31

The common timestamp elements are '%Y-%m-%d %H:%M:%S'. For more details on date format, see ?

Also see Time, Date and Timestamp Parsing with dateutil below.

Formating as String

d = ...
d.strftime("%Y-%m-%d %H:%M")

dateutil Module

https://pypi.org/project/python-dateutil/

Time, Date and Timestamp Parsing with dateutil

The datetime package documentation seems to recommend the dateutil for time zone support and parsing.

import dateutil.parser as du

d = du.parse('10/01/2023')
assert d.year == 2023
assert d.month == 10
assert d.day == 1

time Module

Sleep

from time import sleep
sleep(1)