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)