Almost all the built-in time and date functions in Python come from the time and datetime modules. The following describes the usage of the time and datetime modules in Python. For more information, see
Time Module
The time module is a function that contains time operations. although these functions are often valid, not all methods are valid on any platform. time is represented by struct_time.
import time# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24, tm_hour=14, tm_min=17, tm_sec=26, tm_wday=4, tm_yday=114, tm_isdst=0)# 2015print time.localtime()print time.localtime().tm_year
Function
Time. time (): returns a timestamp.
Time. asctime ([t]): convert the tuples or struct_time returned by gmtime () and localtime () to string.
Time. clock (): return the time when the program is running during the first call. The interval between the second call and the previous one is returned.
Time. ctime ([secs]): converts a timestamp to a time string. if not provided, the current time string is returned, which is the same as asctime (localtime.
Time. gmtime ([secs]): converts a timestamp to struct_time in the UTC time zone.
Time. localtime ([secs]): Similar to gmtime (), but it is converted to the local time zone.
Time. mktime (t): struct_time is converted to a timestamp.
Time. sleep (secs): specifies the thread delay time, in seconds.
Time. strftime (format [, t]): converts a sturc_time or tuples as strings based on parameters.
Time. strptime (string [, format]): returns a struct_time.
Import time # Fri Apr 24 06:39:34 2015 print time. asctime (time. gmtime () #0.0 # None #1.01136392961 print time varies by computer. clock () print time. sleep (1) print time. clock () # Fri Apr 24 14:42:07 2015 print time. ctime () # 2015-04-24print time. strftime ('% Y-% m-% d', time. localtime () #1429857836.0 print time. mktime (time. localtime ())
Common formatting strings in the time module
Datetime module
The datetime module provides simple or complex operations on dates and times. The datetime module provides the following Available Types (Available Types ).
Datetime. MINYEAR and datetime. MAXYEAR module constants indicate the range accepted by datetime.
Class datetime. date: an idealized date that provides the year, month, and day attributes.
Class datetime. time: an idealized time that provides hour, minute, second, microsecond, tzinfo.
Class datetime. datetime: a combination of date and time. year, month, day, hour, minute, second, microsecond, tzinfo are provided.
Class datetime. timedelta: represents the subtle differences between two dates, time, and datetime duration.
Class datetime. tzinfo: abstract base class of the time object.
from datetime import timedelta, datetimea = datetime.now()b = timedelta(days=7)# 7 days, 0:00:00# 2015-04-14 16:02:39.189000print bprint a - b
The following describes the methods of classes and classes.
Date class
A date object represents an idealized date.
Class datetime. date (year, month, day) # All arguments are required. arguments may be ints or longs. # All parameters are required. the parameter may be int or long. MINYEAR <= year <= MAXYEAR 1 <= month <= 12 1 <= day <= number of days in the given month and year. (With Month and year)
ValueError is thrown if the parameter is out of the given range.
1. class method> 'date. today () ': returns the current local date, which is equivalent to 'date. fromtimestamp (time. time ())'.
Return the current local date. This is equvalent to 'date. fromtimestamp (time. time ())'.
from datetime import date # print 2015-04-21 print date.today()
2. date. fromtimestamp (timestamp): returns the local date based on the provided timestamp. the timestamp is usually used for time-type storage.
import timefrom datetime import date# 1429587111.21# 2015-04-21print time.time()print date.fromtimestamp(time.time())
3. date. fromordinal (ordinal): return date based on the provided Gregorian calendar (not described)
Class attributes
Date. min: Returns date (MINYEAR, 1, 1 ).
Date. max: Returns date (MAXYEAR, 12, 31 ).
Date. year: returns the year, between MINYEAR and MAXYEAR.
Date. month: returns the month, between 1 and December.
Date. day: returns the value range from 1 to n.
d = date(2014, 4, 21)# 2014 4 21print d.year, d.month, d.day
Instance method
Date. replace (year, month, day): returns a data object with the same value. in addition to these parameters, a new value is specified for the keyword.
Date. timetuple (): returns a time. struct_time object.
Date. toordinal (): returns a Gregoian Calendar object.
Date. weekday (): return day of the week. Monday is 0, and Sunday is 6.
Date. isoweekday (): returns day of the week. Monday is 1, and Sunday is 7.
Date. isocalendar (): returns a triple (ISO year, ISO week number, ISO weekday ).
Date. isoformat (): returns the string format of 'yyyy-MM-DD.
Date. ctime (): returns a string date. d. ctime () is equivalent to time. ctime (time. mktime (d. timetuple ())).
Date. strftime (format): returns a string date in custom format.
d = date(2015, 4, 21)# 2015-04-21# 2015-04-21# 2015-04-22print dprint d.replace()print d.replace(day=22)# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1)print d.timetuple()# print 1# print 2print d.weekday()print d.isoweekday()# print 2015-04-21print d.isoformat()# print 21/04/2015print d.strftime('%d/%m/%y')
Datetime type
A datetime object is a single object that contains information about all date and time objects.
class datetime.datetime(year, month, day[, hour [, minute [, second [, microsecond [, tzinfo]]]]]) # The year, month and day arguments are required. MINYEAR <= year <= MAXYEAR 1 <= month <= 12 1 <= day <= n 0 <= hour < 24 0 <= minute < 60 0 <= second < 60 0 <= microsecond < 10**6
Class method
Datetime. today (): returns the current local datetime. with tzinfo None. this is equivalent to datetime. fromtimestamp (time. time ()).
Datetime. now ([tz]): returns the current local date and time. if the optional parameter tz is None or is not described in detail, this method will be like today ().
Datetime. utcnow (): returns the current UTC date and time. if tzinfo None, it is similar to now.
Datetime. fromtimestamp (timestamp [, tz]): returns the local date and time based on the timestamp. tz specifies the time zone.
Datetime. utcfromtimestamp (timestamp): returns UTC datetime based on the timestamp.
Datetime. fromordinal (ordinal): return datetime according to Gregorian ordinal.
Datetime. combine (date, time): returns a new datetime based on date and time.
Datetime. strptime (date_string, format): return a datetime according to date_string and format.
from datetime import datetime# 2015-04-21 14:07:39.262000print datetime.today()# 2015-04-21 14:08:20.362000print datetime.now()# 1429596607.06# 2015-04-21 14:10:07.061000t = time.time() print tprint datetime.fromtimestamp(t)from datetime import datetime, date, timea = date(2015, 4, 21)b = time(14, 13, 34)# 2015-04-21 14:13:34print datetime.combine(a, b)
Instance method
Datetime. date (): returns the date object of the same year, month, and day.
Datetime. time (): returns the time object of the same time, minute, second, and second.
Datetime. replace (kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], similar to date.
For other methods, see the official documentation...
from datetime import datetime, date, timetd = date(2015, 4, 21)n = time(14, 28, 30)# 2099-04-21 14:30:42.103000print datetime.now(0.replace(year=2099)
Class attributes
Datetime. min: datetime (MINYEAR, 1, 1 ).
Datetime. max: datetime (MAXYEAR, 12, 31, 23, 59, 59,999 999 ).
Read-only)
Datetime. year: 1 to 9999.
Datetime. month: 1 to 12
Datetime. day: 1 to n
Datetime. hour: In range (24). 0 to 23
Datetime. minute: In range (60 ).
Datetime. second: In range (60 ).
Datetime. microsecond: In range (1000000 ).
Time class
Time indicates the local time (within one day.
Class datetime. time ([hour [, minute [, second [, microsecond [, tzinfo]) # All arguments are optional. # All parameters are optional. 0 <= hour <24 0 <= minute <60 0 <= second <60 0 <= microsesond <10 ** 6
The time class is some operations on time. its functions are similar to datetime. In fact, date and time are operations on date and time in datetime.
For more articles about the time module and datetime module in Python, refer to the PHP Chinese network!