class datetime – Represent a date and time
- class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
A datetime object is a single object containing all the information from a
date
object and atime
object.The arguments are as follows:
year
,month
,day
,hour
,minute
,second
andmicrosecond
are integers in the given ranges:1
<=month
<=12
1
<=day
<= number of days in the given month and year0
<=hour
<24
0
<=minute
<60
0
<=second
<60
0
<=microsecond
<1000000
Each of these arguments will default to
0
if omitted.tzinfo
is adatetime.tzinfo
object, allowing a datetime to be associated with a particular timezone. If this is not desired, thetzinfo
can beNone
.fold
is a value of either0
or1
used to disambiguate wall times during a repeated interval. A repeated interval occurs when clocks are rolled back at the end of daylight saving time, or when the UTC offset for the current zone is decreased for political reasons. The values0
and1
represent, respectively, the earlier and later of the two moments with the same wall time representation.
- classmethod fromisoformat()
Construct a datetime object from a string in ISO 8601 format:
from datetime import datetime dt = datetime.fromisoformat('2012-12-21 07:55:27.999999')
- classmethod fromordinal(ordinal)
Construct a datetime object representing the year, month and day specified by the provided
ordinal
.An
ordinal
is an integer representing the number of days since January 1st of year 1.The
hour
,minute
,second
, andmicrosecond
arguments will all be set to0
, andtzinfo
will be set toNone
.
- classmethod fromtimestamp(timestamp, tz=None)
Construct a datetime object representing the year, month and day specified by the provided
timestamp
.A
timestamp
in this case is a floating point number representing the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC).An optional
tz
argument may be provided to specify the timezone of the resulting datetime.
- classmethod now(tz=None)
Construct a datetime object representing the current date and time.
An optional
tz
argument may be provided to specify the timezone of the resulting datetime.
- classmethod combine(date, time, tzinfo=None)
Construct a datetime object from a date and time object.
The
date
andtime
arguments must be instances ofdate
andtime
, respectively.The
tzinfo
argument is optional and will be used to set the timezone of the resulting datetime. If not provided, the timezone (or lack thereof) will be inferred from thetime
argument.
- month
The month of the datetime, an integer in the range
1
to12
.
- day
The day of the datetime, an integer in the range
1
to the number of days in the month represented bymonth
.
- hour
The hour of the datetime, an integer in the range
0
to23
.
- minute
The minute of the datetime, an integer in the range
0
to59
.
- second
The second of the datetime, an integer in the range
0
to59
.
- microsecond
The microsecond of the datetime, an integer in the range
0
to999999
.
- tzinfo
The
datetime.tzinfo
object associated with the datetime, orNone
if no timezone information is associated with the datetime.
- fold
Used to disambiguate wall times during a repeated interval. The values
0
and1
represent, respectively, the earlier and later of the two moments with the same wall time representation.
- replace(year=self.year, month=self.year, day=self.year, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, *, fold=0)
Return a new
datetime
object with the same values as the existing datetime object, but with the specified parameters updated.
- tuple()
Return the datetime as a 9-tuple
(year, month, day, hour, minute, second, microsecond, tzinfo, fold)
.
- astimezone(tz=None)
Return a datetime that represents the same point in time, relative to the specified timezone
tz
. The current datetime must already have an associatedtzinfo
, otherwise this method will raise aNotImplementedError
.
- date()
Return the date portion of the datetime as a
date
object. The resulting object will have no timezone information.
- time()
Return the time portion of the datetime as a
time
object. The resulting object will have no timezone information.
- timetz()
Return the time portion of the datetime as a
time
object. The resulting object will have the same timezone information as the original datetime.
- dst()
Return the daylight savings time offset as a
datetime.timedelta
object, orNone
if the time has no associated timezone.
- isoformat(sep='T', timespec='auto')
Return a string representing the datetime in ISO 8601 format.
By default, this will output the date and time in the format
YYYY-MM-DDTHH:MM:SS
(orYYYY-MM-DDTHH:MM:SS.ssssss
ifmicrosecond
is not0
). The optionalsep
argument can be used to specify the separator between the date and time portions of the output string. The optionaltimespec
argument can be used to specify the number of digits to include in the output string:'hours'
:YYYY-MM-DDTHH
'minutes'
:YYYY-MM-DDTHH:MM
'seconds'
:YYYY-MM-DDTHH:MM:SS
'milliseconds'
:YYYY-MM-DDTHH:MM:SS.sss
'microseconds'
:YYYY-MM-DDTHH:MM:SS.ssssss
- isoweekday()
Return the day of the week as an integer, where Monday is
1
and Sunday is7
.
- weekday()
Return the day of the week as an integer, where Monday is
0
and Sunday is6
.
- timetuple()
Return the datetime as a 9-tuple
(year, month, day, hour, minute, second, weekday, yearday, dst)
.In this case:
weekday
is the day of the week as an integer, where Monday is0
and Sunday is6
.yearday
is the day of the year as an integer, where January 1st is1
.dst
is-1
if there is no associated timezone,0
if the associated timezone does not observe daylight savings time, or1
if it does.
- toordinal()
Return an integer representing the ordinal of the date, where January 1st of year 1 has ordinal
1
.
- tzname()
Return the name of the timezone as a string, or
None
if the datetime has no associated timezone.
- utcoffset()
Return the UTC offset as a
datetime.timedelta
object, orNone
if the datetime has no associated timezone.