This article is to share in Python the time module datetime usage method, considerations and simple examples, the need for small partners can refer to the following
The datetime module is used for a collection of date and time modules, with DateTime having two constants, Maxyear and Minyear, 9999 and 1, respectively.
The DateTime module defines 5 classes, each of which is
1.datetime.date: A class that represents a date
2.datetime.datetime: A class that represents a date time
3.datetime.time: A class that represents time
4.datetime.timedelta: Represents a time interval, that is, an interval of two points in time
5.datetime.tzinfo: Information about the time zone
First, take a look at the Datetime.date class:
The date class has three parameters, Datetime.date (year,month,day), and returns Year-month-day
Method:
1.datetime.date.ctime (), return format as Sun APR 16 00:00:00 2017
2.datetime.date.fromtimestamp (timestamp), returns a Date object based on a given time timestamp, Datetime.date.today () has the same effect
3.datetime.date.isocalendar (): Returns a tuple of the format (Year,month,day), (2017, 15, 6)
4.datetime.date.isoformat (): Returns a format such as Yyyy-mm-dd
5.datetime.date.isoweekday (): Returns the week of the given date (0-6), Monday = 0, Sunday =6
6.datetime.date.replace (Year,month,day): replaces the given date but does not change the original date
7.datetime.date.strftime (format): Formats the DateTime according to the given format.
8.datetime.date.timetuple (): Returns the Time.struct_time object corresponding to the date
Time.struct_time (tm_year=2017, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=105, tm_isdst=- 1)
9.datetime.date.weekday (): Return day of the week
Python format symbols in time Date:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself
Second, take a look at the DateTime class
The time class has 5 parameters, Datetime.time (hour,minute,second,microsecond,tzoninfo), and returns 08:29:30
1.datetime.time.replace ()
2.datetime.time.strftime (format): Returns time in format
3.datetime.time.tzname (): Returns the time zone name
4.datetime.time.utcoffset (): Time offset for return time zone
Iii. datetime Classes of datetime
The DateTime class has a number of parameters, DateTime (year, month, day[, hour[, minute[, second[, Microsecond[,tzinfo]]), and returns date, time, and seconds
Datetime.datetime.ctime ()
Datetime.datetime.now (). Date (): Returns the day part of the current datetime
Datetime.datetime.now (). Time (): Returns the part of the current date time
Datetime.datetime.fromtimestamp ()
Datetime.datetime.now (): Returns the current system time
Datetime.datetime.replace ()
Datetime.datetime.strftime (): Converted from date format to string format
Datetime.datetime.now (). Strftime ('%b-%d-%y%h:%m:%s ')
' apr-16-2017 21:01:35 '
Datetime.datetime.strptime (): converted from string format to date format
Datetime.datetime.strptime (' apr-16-2017 21:01:35 ', '%b-%d-%y%h:%m:%s ')
2017-04-16 21:01:35
Iv. Timedelta class for DateTime
The Datetime.datetime.timedelta is used to calculate the difference between two dates, for example:
>>> A=datetime.datetime.now () >>> b=datetime.datetime.now () >>> adatetime.datetime (2017, 4, (4, 871000) >>> bdatetime.datetime (+, +, +, 603000) >>> B-adatetime.timedel TA (0, 8, 732000) >>> (b-a). Seconds8
Or
TIME1 = Datetime.datetime (2015, 11, 2) "" "Calculates the difference in days" "" Print ( time1-time2). Days "" Calculates the number of seconds between two dates "" "Print (time1-time2). Total_seconds ()