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
dateobject and atimeobject.The arguments are as follows:
year,month,day,hour,minute,secondandmicrosecondare integers in the given ranges:1<=month<=121<=day<= number of days in the given month and year0<=hour<240<=minute<600<=second<600<=microsecond<1000000
Each of these arguments will default to
0if omitted.tzinfois adatetime.tzinfoobject, allowing a datetime to be associated with a particular timezone. If this is not desired, thetzinfocan beNone.foldis a value of either0or1used 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 values0and1represent, 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
ordinalis an integer representing the number of days since January 1st of year 1.The
hour,minute,second, andmicrosecondarguments will all be set to0, andtzinfowill be set toNone.
- classmethod fromtimestamp(timestamp, tz=None)
 Construct a datetime object representing the year, month and day specified by the provided
timestamp.A
timestampin this case is a floating point number representing the number of seconds since the epoch (January 1, 1970, 00:00:00 UTC).An optional
tzargument 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
tzargument 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
dateandtimearguments must be instances ofdateandtime, respectively.The
tzinfoargument 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 thetimeargument.
- month
 The month of the datetime, an integer in the range
1to12.
- day
 The day of the datetime, an integer in the range
1to the number of days in the month represented bymonth.
- hour
 The hour of the datetime, an integer in the range
0to23.
- minute
 The minute of the datetime, an integer in the range
0to59.
- second
 The second of the datetime, an integer in the range
0to59.
- microsecond
 The microsecond of the datetime, an integer in the range
0to999999.
- tzinfo
 The
datetime.tzinfoobject associated with the datetime, orNoneif no timezone information is associated with the datetime.
- fold
 Used to disambiguate wall times during a repeated interval. The values
0and1represent, 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
datetimeobject 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
dateobject. The resulting object will have no timezone information.
- time()
 Return the time portion of the datetime as a
timeobject. The resulting object will have no timezone information.
- timetz()
 Return the time portion of the datetime as a
timeobject. The resulting object will have the same timezone information as the original datetime.
- dst()
 Return the daylight savings time offset as a
datetime.timedeltaobject, orNoneif 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.ssssssifmicrosecondis not0). The optionalsepargument can be used to specify the separator between the date and time portions of the output string. The optionaltimespecargument 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
1and Sunday is7.
- weekday()
 Return the day of the week as an integer, where Monday is
0and Sunday is6.
- timetuple()
 Return the datetime as a 9-tuple
(year, month, day, hour, minute, second, weekday, yearday, dst).In this case:
weekdayis the day of the week as an integer, where Monday is0and Sunday is6.yeardayis the day of the year as an integer, where January 1st is1.dstis-1if there is no associated timezone,0if the associated timezone does not observe daylight savings time, or1if 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
Noneif the datetime has no associated timezone.
- utcoffset()
 Return the UTC offset as a
datetime.timedeltaobject, orNoneif the datetime has no associated timezone.