datetime
It is a built-in module of python, used to process date and time.
The commonly used classes of this module are:
Class name | Function Description |
---|---|
date | Date object |
time | Time object |
datetime | Datetime object |
timedelta | time interval |
tzinfo | Time zone information object |
datetime
How datetime
to use the classes in the module . datetime
Date and time the object is a combination of covers date
and time
all the information of the object.
One, import datetime
class
The first step is to import the datetime
class:
from datetime import datetime
Second, construct the datetime
object
datetime(year,month,day,hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
Parameter range:
-
MINYEAR <= year <= MAXYEAR
-
1 <= month <= 12
-
1 <= day <= Specified year and month celestial number
-
0 <= hour < 24
-
0 <= minute < 60
-
0 <= second < 60
-
0 <= microsecond < 1000000
Examples:
datetime(2019,12,6,13,30,50)
# 输出 : datetime.datetime(2019, 12, 6, 13, 30, 50)
Three, class method
-
datetime.today()
Returns the current date and time datetime
object in this region
datetime.today()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
-
datetime.now(tz=None)
Returns the current date and time datetime
object in the region , you can add a time zone tz When there is no time zone by default, the returned result is the datetime.today()
same
datetime.now()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
-
datetime.utcnow()
Returns UTC (same as Greenwich Mean Time) date and time datetime
object
datetime.now()
# 输出 : datetime.datetime(2019, 12, 9, 13, 27, 54, 693978)
-
datetime.fromtimestamp(timestamp, tz=None)
Returns the datetime
object of the given timestamp , the time zone is empty by default, returns the local date and time, or you can specify the time zone
datetime.fromtimestamp(1575877756)
# 输出 : datetime.datetime(2019, 12, 9, 15, 49, 16)
If you want to get the current timestamp, you can use the time
module time
method:
import time
time.time()
# 输出:1575877756.4673727
-
datetime.utcfromtimestamp(timestamp)
Returns the datetime
object with a given timestamp , the date and time default to UTC (same as Greenwich Mean Time)
datetime.fromtimestamp(1575877756)
# 输出 : datetime.datetime(2019, 12, 9, 7, 49, 16)
-
datetime.fromordinal(ordinal)
The input parameter is the number of days, and it returns the year, month, and day datetime
object counted from 0 AD. For example, if the input parameter is 365, it will return 1-12-31
datetime.fromordinal(365)
# 输出 : datetime.datetime(1, 12, 31, 0, 0)
-
datetime.combine(date, time, tzinfo=self.tzinfo)
Splice date
and time
object to form a new datetime
object. You can enter the time zone parameter, otherwise it will default to the original time
time zone
date_ = datetime.today().date()
time_ = datetime.today().time()
datetime.combine(date_,time_)
# 输出 : datetime.datetime(2019, 12, 9, 16, 12, 56, 914484)
-
datetime.strptime(date_string, format)
Convert the formatted date and time string into an datetime
object, you can convert date, time, date and time
datetime.strptime('2019-11-05','%Y-%m-%d')
# 输出 : datetime.datetime(2019, 11, 5, 0, 0)
datetime.strptime('09:30:50','%H:%M:%S')
# 输出 : datetime.datetime(1900, 1, 1, 9, 30, 50)
datetime.strptime('2019-11-05 09:30:50','%Y-%m-%d %H:%M:%S')
# 输出 : datetime.datetime(2019, 11, 5, 9, 30, 50)
Four, instance method
-
datetime.date()
Return date
object
d = datetime(2019,12,6,13,30,50)
d.date()
# 输出 : datetime.date(2019, 12, 6)
-
datetime.time()
Return time
object
d = datetime(2019,12,6,13,30,50)
d.time()
# 输出 : datetime.time(13, 30, 50)
-
datetime.timestamp()
datetime
Return a timestamp for the given object
d = datetime(2019,12,6,13,30,50)
d.timestamp()
# 输出 : 1575610250.0
-
datetime.weekday()
Returns the day of the week, Monday is 0, Sunday is 6
d = datetime(2019,12,6,13,30,50)
d.weekday()
# 输出 : 4
-
datetime.isoweekday()
Returns the day of the week, Monday is 1 and Sunday is 7
d = datetime(2019,12,6,13,30,50)
d.isoweekday()
# 输出 : 5
-
datetime.isocalendar()
Return array: (year, week number, day of week)
d = datetime(2019,12,6,13,30,50)
d.isocalendar()
# 输出 : (2019, 49, 5)
-
datetime.ctime()
Returns the string representation of the date and time
d = datetime(2019,12,6,13,30,50)
d.ctime()
# 输出 : 'Fri Dec 6 13:30:50 2019'
-
datetime.strftime(date_string, format)
Convert datetime
object to format string
d = datetime.today()
datetime.strftime(d,'%Y-%m-%d %H:%M:%S')
# 输出 : '2019-12-09 16:32:18'
-
datetime.toordinal()
Returns the datetime
number of days from 0 AD to the specified
d = datetime(2019,12,6,13,30,50)
d.toordinal()
# 输出 : 737399
Five, class instance attributes
-
datetime.year
Returns datetime
the year of the given object
d = datetime(2019,12,6,13,30,50)
d.year
# 输出 : 2019
-
datetime.month
Returns datetime
the month of the given object
d = datetime(2019,12,6,13,30,50)
d.month
# 输出 : 12
-
datetime.day
Returns the datetime
number of days of the given object (day of the month)
d = datetime(2019,12,6,13,30,50)
d.day
# 输出 : 6
-
datetime.hour
Returns the datetime
number of hours (time of day) for the given object
d = datetime(2019,12,6,13,30,50)
d.hour
# 输出 : 13
-
datetime.minute
Returns the datetime
number of minutes (a minute of an hour) for a given object
d = datetime(2019,12,6,13,30,50)
d.minute
# 输出 : 30
-
datetime.second
Returns datetime
the number of seconds of the given object (a second of a minute)
d = datetime(2019,12,6,13,30,50)
d.second
# 输出 : 50
-
datetime.microsecond
Returns the datetime
number of milliseconds for the given object (a second in a minute)
d = datetime(2019,12,6,13,30,50,3000)
d.microsecond
# 输出 : 3000
-
datetime.tzinfo
Returns datetime
the time zone of the object, provided that the parameters datetime
need to be passed in when creating the object tzinfo
, if not passed in, the return value is None
.
# 获取一个含有时区的datetime对象
import pytz
sh = pytz.timezone('Asia/Shanghai')
d = datetime(2018, 11, 1, hour=8, tzinfo=sh)
d.tzinfo
# 输出 : <DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>
6. Conclusion
datetime
datetime
Various methods and attributes of the classes in the module, rich code cases, divided into categories, easy to find when using.