1. Two ways to get the current time:
Copy Code code as follows:
Import Datetime,time
now = Time.strftime ("%y-%m-%d%h:%m:%s")
Print now
now = Datetime.datetime.now ()
Print now
2. Get the last day of last month's date (minus 1 days for the first day of the month)
Copy Code code as follows:
Last = Datetime.date (Datetime.date.today (). Year,datetime.date.today (). month,1)-datetime.timedelta (1)
Print Last
3. Get time difference (seconds, often used to calculate the operation of the program)
Copy Code code as follows:
StartTime = Datetime.datetime.now ()
#long Running
Endtime = Datetime.datetime.now ()
Print (endtime-starttime). seconds
4. Calculate the current time back 10 hours
Copy Code code as follows:
D1 = Datetime.datetime.now ()
D3 = D1 + Datetime.timedelta (hours=10)
D3.ctime ()
The classes used in this class are: DateTime and Timedelta two. They can add and subtract from each other. Each class has methods and properties to view specific values, such as datetime can view: Days, hours (hour), Weeks (Weekday ()), Timedelta can view: days, seconds (seconds), and so on. &NBSP
5.python Time date formatting symbols:
%y Two-digit year representation (00-99)
%y Four-digit year representation (000-9999)
%m Month (01-12)
Day in%d months (0-31)
% H 24 hours (0-23)
%I 12 hours per hour (01-12)
% M minutes (00=59)
%s seconds (00-59)
%a Local simplified week name
% a local full week name
%b locally simplified month name
% b local full month name
%c Local corresponding date representation and time representation
%j One day of the year (001-366)
%p Local a.m. or p.m. Equivalent
% U in the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday is the beginning of the week
% W The number of weeks in a year (00-53) Monday is the start of the week
%x Local corresponding date represents
% x local time indication
%Z The current time zone name
%% itself