Time, Date, Timestamp in Python: Difference between revisions
(7 intermediate revisions by the same user not shown) | |||
Line 21: | Line 21: | ||
The <code>datetime</code> package documentation seems to recommend the <code>[[#dateutil|dateutil]]</code> for time zone support and parsing. | The <code>datetime</code> package documentation seems to recommend the <code>[[#dateutil|dateutil]]</code> for time zone support and parsing. | ||
==<span id='datetime.datetime'></span>The <tt>datetime.datetime</tt> Type== | |||
==Time Interval with <tt>timedelta</tt>== | ==Time Interval with <tt>timedelta</tt>== | ||
Line 49: | Line 51: | ||
current_time = now.strftime("%H:%M:%S") | current_time = now.strftime("%H:%M:%S") | ||
print("Current Time =", current_time) | print("Current Time =", current_time) | ||
</syntaxhighlight> | |||
==Current Date== | |||
With <code>now()</code>: | |||
<syntaxhighlight lang='py'> | |||
from datetime import datetime | |||
d = datetime.now().strftime("%Y-%m-%d") | |||
print(d) # displays YYYY-mm-dd | |||
</syntaxhighlight> | |||
With <code>date.today()</code>: | |||
print(str( | <syntaxhighlight lang='py'> | ||
from datetime import date | |||
print(str(date.today())) # displays YYYY-mm-dd | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 72: | Line 81: | ||
The common timestamp elements are '%Y-%m-%d %H:%M:%S'. <font color=darkkhaki>For more details on date format, see ?</font> | The common timestamp elements are '%Y-%m-%d %H:%M:%S'. <font color=darkkhaki>For more details on date format, see ?</font> | ||
Also see [[#Time.2C_Date_and_Timestamp_Parsing_with_dateutil|Time, Date and Timestamp Parsing with <tt>dateutil</tt>]] below. | |||
=<SPAN ID='dateutil'></SPAN><tt>dateutil</tt> Module= | =<SPAN ID='dateutil'></SPAN><tt>dateutil</tt> Module= | ||
{{External|https://pypi.org/project/python-dateutil/}} | {{External|https://pypi.org/project/python-dateutil/}} | ||
==<span id='Time.2C_Date_and_Timestamp_Parsing'></span>Time, Date and Timestamp Parsing with <tt></tt>== | ==<span id='Time.2C_Date_and_Timestamp_Parsing'></span>Time, Date and Timestamp Parsing with <tt>dateutil</tt>== | ||
The <code>[[#datetime|datetime]]</code> package documentation seems to recommend the <code>dateutil</code> for time zone support and parsing. | The <code>[[#datetime|datetime]]</code> package documentation seems to recommend the <code>dateutil</code> for time zone support and parsing. | ||
Line 86: | Line 97: | ||
assert d.day == 1 | assert d.day == 1 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 03:35, 18 October 2023
Internal
Overview
Time, date and timestamps are handled with the time
and datetime
modules, available in the Standard Library.
Additionally, the dateutil
module provides useful extensions.
time Module
Sleep
from time import sleep
sleep(1)
datetime Module
datetime Overview
The datetime
module provides classes for manipulating dates and times.
https://docs.python.org/3/library/datetime.html#module-datetime
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.datetime(2022,3,27,13,27,45,46000)
dt2 = datetime.datetime(2022,6,30,14,28)
tdelta = dt2 - dt1
print(tdelta)
print(type(tdelta))
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
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.
dateutil Module
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