Differences between revisions 3 and 10 (spanning 7 versions)
Revision 3 as of 2023-04-11 18:51:42
Size: 2286
Comment:
Revision 10 as of 2023-06-15 18:29:46
Size: 2969
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

'''`datetime`''' is a module providing utilities and APIs for working with dates, times, and timezones.
Line 17: Line 19:
== Datetime ==

See [[Python/Datetime/Datetime|here]].

----



== Time ==

See [[Python/Datetime/Time|here]].

----


Line 19: Line 37:
A `datetime.timedelta` object stores some duration of time as a tuple of days, seconds, and milliseconds. See [[Python/Datetime/TimeDelta|here]].
Line 21: Line 39:
It is constructed as: ----



== TimeZone ==

A `datetime.timezone` represents a fixed offset from UTC.

To access the UTC timezone, use the class attribute `datetime.timezone.utc`.
Line 26: Line 52:
t1 = datetime.timedelta(days=3, seconds=2, milliseconds=1) est = datetime.timezone(datetime.timedelta(hours=-5))
Line 33: Line 59:
=== Operations === == TZInfo ==
Line 35: Line 61:
`datetime.timedelta` objects can be used with the following [[Python/Builtins/Operators|operators]] and [[Python/Builtins/Functions|functions]].

||'''Operation''' ||'''Meaning''' ||
||`t1 + t2` ||sum ||
||`t1 - t2` ||difference ||
||`t1 * i` ||multiplication by integer ||
||`t1 * f` ||multiplication by float rounded to nearest even microsecond ||
||`t1 / t2` ||division of `t1` in terms of `t2`, returning a float ||
||`t1 / n` ||division by a number rounded to nearest even microsecond ||
||`t1 // i` ||floored division by an integer ||
||`t1 // t2` ||floored division of `t1` in terms of `t2`, returning an integer||
||`t1 % t2` ||the remainder of `t1` from `t1 // t2` ||
||`q, r = divmod(t1, t2)`||`q = t1 // t2` and `r = t1 % t2` ||
||`-t1` ||`datetime.timedelta(-t1.days, -t1.seconds, -t1.microseconds)` ||
||`abs(t1)` ||`t` if t >= 0 else -t`` ||
||`str(t1)` ||returns a string like `[D day[s], ][H]H:MM:SS[.UUUUUU]` ||
||`repr(t1)` ||returns a string constructor call ||

Some of these operations are not safe from overflows.
An abstract base class for `datetime.timezone`. Not to be used directly.
Line 59: Line 67:
=== Total_Seconds === == StrFTime and StrPTime ==
Line 61: Line 69:
Convert the stored days, seconds, and milliseconds into just seconds.
Line 63: Line 70:
Equivalent to: ||'''Directive'''||'''Meaning'''||'''Example Values'''||
||`%a`||weekday in locale abbreviated name||Sun||
||`%A`||weekday in locale full name||Sunday||
||`%w`||weekday as an integer||0 ''(as Sunday)'', 6 ''(as Saturday)''||
||`%d`||day of the month as a zero-padded integer||01||
||`%b`||month in locale abbreviated name||Jan||
||`%B`||month in locale full name||January||
||`%m`||month as a zero-padded integer||01||
||`%y`||year as a zero-padded two-digit integer||00||
||`%Y`||year as a zero-padded four-digit integer||0001||
||`%H`||24-hour clock hour as a zero-padded integer||00||
||`%I`||12-hour clock hour as a zero-padded integer||01||
||`%p`||locale equivalent of AM/PM.||AM||
||`%M`||minute as a zero-padded integer||00||
||`%S`||second as a zero-padded integer||00||
||`%f`||microsecond as a zero-padded six-digit integer||000000||
||`%z`||UTC offset in the form ±HHMM[SS[.ffffff]], or empty if the object is naive||+0000, -0400, +1030, +063415, -030712.345216||
||`%Z`||time zone name, or empty if the object is naive||UTC||
||`%j`||day of the year as a zero-padded integer||001||
||`%U`||week of the year as a zero-padded two-digit integer; all days preceding the first Sunday are in week 0||00||
||`%W`||week of the year as a zero-padded two-digit integer; all days preceding the first Monday are in week 0||00||
||`%c`||locale datetime representation||Tue Aug 16 21:30:00 1988||
||`%x`||locale date representation||08/16/1988||
||`%X`||locale time representation||21:30:00||
||`%%`||literal %|| ||
||`%G`||year that contains the greater part of the ISO week (`%V`) zero-padded four-digit integer||0001||
||`%u`||weekday as an integer||1 ''(as Monday)'', 7 ''(as Sunday)''||
||`%V`||week of the year as a zero-padded two-digit number; week 01 is the week beginning on a Monday that contains January 4th; all preceding days belong to the prior year||01||
Line 65: Line 99:
{{{
import datetime
----
Line 68: Line 101:
t1 = datetime.timedelta(days=3)
Line 70: Line 102:
seconds = t1 / datetime.timedelta(seconds=1)
}}}

== See also ==

[[https://docs.python.org/3/library/datetime.html|Python datetime module documentation]]

[[https://pymotw.com/3/datetime/|Python Module of the Day article for datetime]]

Python Datetime

datetime is a module providing utilities and APIs for working with dates, times, and timezones.


Date

See here.


Datetime

See here.


Time

See here.


TimeDelta

See here.


TimeZone

A datetime.timezone represents a fixed offset from UTC.

To access the UTC timezone, use the class attribute datetime.timezone.utc.

import datetime

est = datetime.timezone(datetime.timedelta(hours=-5))


TZInfo

An abstract base class for datetime.timezone. Not to be used directly.


StrFTime and StrPTime

Directive

Meaning

Example Values

%a

weekday in locale abbreviated name

Sun

%A

weekday in locale full name

Sunday

%w

weekday as an integer

0 (as Sunday), 6 (as Saturday)

%d

day of the month as a zero-padded integer

01

%b

month in locale abbreviated name

Jan

%B

month in locale full name

January

%m

month as a zero-padded integer

01

%y

year as a zero-padded two-digit integer

00

%Y

year as a zero-padded four-digit integer

0001

%H

24-hour clock hour as a zero-padded integer

00

%I

12-hour clock hour as a zero-padded integer

01

%p

locale equivalent of AM/PM.

AM

%M

minute as a zero-padded integer

00

%S

second as a zero-padded integer

00

%f

microsecond as a zero-padded six-digit integer

000000

%z

UTC offset in the form ±HHMM[SS[.ffffff]], or empty if the object is naive

+0000, -0400, +1030, +063415, -030712.345216

%Z

time zone name, or empty if the object is naive

UTC

%j

day of the year as a zero-padded integer

001

%U

week of the year as a zero-padded two-digit integer; all days preceding the first Sunday are in week 0

00

%W

week of the year as a zero-padded two-digit integer; all days preceding the first Monday are in week 0

00

%c

locale datetime representation

Tue Aug 16 21:30:00 1988

%x

locale date representation

08/16/1988

%X

locale time representation

21:30:00

%%

literal %

%G

year that contains the greater part of the ISO week (%V) zero-padded four-digit integer

0001

%u

weekday as an integer

1 (as Monday), 7 (as Sunday)

%V

week of the year as a zero-padded two-digit number; week 01 is the week beginning on a Monday that contains January 4th; all preceding days belong to the prior year

01


See also

Python datetime module documentation

Python Module of the Day article for datetime


CategoryRicottone

Python/Datetime (last edited 2023-06-15 18:29:46 by DominicRicottone)