Python time processing 1. two methods to get the current time:
Import datetime, time
Now = time. strftime ("% Y-% m-% d % H: % M: % S ")
Print now
Now = datetime. datetime. now ()
Print now
2. get the date of the last day of the last month (the first day of the month minus one day)
Last = datetime. date (datetime. date. today (). year, datetime. date. today (). month, 1)-datetime. timedelta (1)
Print last
3. get the time difference (unit: seconds, usually used for calculating the time when the program runs)
Starttime = datetime. datetime. now ()
# Long running
Endtime = datetime. datetime. now ()
Print (endtime-starttime). seconds
4. calculate the last 10 hours of the current time.
D1 = datetime. datetime. now ()
D3 = d1 + datetime. timedelta (hours = 10)
D3.ctime ()
Common classes include datetime and timedelta. They can be added or subtracted from each other. Each class has some methods and attributes to view specific values, such as datetime: day, hour, and weekday; timedelta can be viewed as days and seconds.
5. time and date formatting symbols in python:
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d Month (0-31)
% H Hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)
% A local simplified week name
% A Local full week name
% B local simplified month name
% B local full month name
% C local date and time
% J one day in the year (001-366)
% P local equivalent of A. M. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date representation
% X local time representation
% Z current time zone name
% Itself
Attached sample code:
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> #-*-coding: UTF-8 -*-
Import datetime, calendar
Def getYesterday ():
Today = datetime. date. today ()
Oneday = datetime. timedelta (days = 1)
Yesterday = today-oneday
Return yesterday
Def getToday ():
Return datetime. date. today ()
# Obtain the date of the previous day of the given parameter and return a list
Def getDaysByNum (num ):
Today = datetime. date. today ()
Oneday = datetime. timedelta (days = 1)
Li = []
For I in range (0, num ):
# Subtract one day from today and one day from one day
Today = today-oneday
# Convert a date to a string
# Result = datetostr (today)
Li. append (datetostr (today ))
Return li
# Convert a string to the datetime type
Def strtodatetime (datestr, format ):
Return datetime. datetime. strptime (datestr, format)
# Convert time to string in the format of 2008-08-02
Def datetostr (date ):
Return str (date) [0: 10]
# How many days are two dates separated, for example, and?
Def datediff (beginDate, endDate ):
Format = "% Y-% m-% d ";
Bd = strtodatetime (beginDate, format)
Ed = strtodatetime (endDate, format)
Oneday = datetime. timedelta (days = 1)
Count = 0
While bd! = Ed:
Ed = ed-oneday
Count + = 1
Return count
# Get all time of two time periods and return list
Def getDays (beginDate, endDate ):
Format = "% Y-% m-% d ";
Bd = strtodatetime (beginDate, format)
Ed = strtodatetime (endDate, format)
Oneday = datetime. timedelta (days = 1)
Num = datediff (beginDate, endDate) + 1
Li = []
For I in range (0, num ):
Li. append (datetostr (ed ))
Ed = ed-oneday
Return li
# Retrieving the current year is a string
Def getYear ():
Return str (datetime. date. today () [0: 4]
# Retrieving the current month is a string
Def getMonth ():
Return str (datetime. date. today () [5: 7]
# Retrieving the current day is a string
Def getDay ():
Return str (datetime. date. today () [8: 10]
Def getNow ():
Return datetime. datetime. now ()
Print getToday ()
Print getYesterday ()
Print getDaysByNum (3)
Print getDays ('2017-10-01 ', '2017-10-05 ')
Print '2017-10-04 00:00:00 '[]
Print str (getYear () + getMonth () + getDay ()
Print getNow ()