Python Time processing detailed

Source: Internet
Author: User
Tags month name timedelta
1. Two ways to get the current time:

Import Datetime,timenow = Time.strftime ("%y-%m-%d%h:%m:%s") Print Nownow = Datetime.datetime.now () print now

2. Get the last day of the month minus 1 days on the first day of the month

Last = Datetime.date (Datetime.date.today (). Year,datetime.date.today (). month,1)-datetime.timedelta (1) Print last

3. Get the time difference (in seconds, often used to calculate when the program is running)

StartTime = Datetime.datetime.now () #long runningendtime = Datetime.datetime.now () print (endtime-starttime). seconds

4. Calculate the current time back 10 hours

D1 = Datetime.datetime.now () d3 = D1 + Datetime.timedelta (hours=10) d3.ctime ()

The classes used in this book are: DateTime and Timedelta two. They can be added and reduced between each other. Each class has some methods and properties to view specific values, such as DateTime can be viewed: Days, hours (hour), Day of the Week (weekday ()), etc. timedelta can view: days, seconds (seconds), etc.

5.python Time Date formatting symbols:

%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

Attach the 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 ()

#获取给定参数的前几天的日期, returns a list

def getdaysbynum (num): Today=datetime.date.today () Oneday=datetime.timedelta (Days=1) li=[] For I in Range (0,num): #今天减一天, one day today=today-oneday #把日期转换成字符串 #result =da Tetostr (Today) Li.append (Datetostr (today)) return Li #将字符串转换成datetime类型 def strtodatetime (datestr,f         ormat): Return Datetime.datetime.strptime (Datestr,format) #时间转换成字符串 in the format 2008-08-02 def datetostr (date): return str (date) [0:10] #两个日期相隔多少天, for example: 2008-10-03 and 2008-10-01 are two days apart def DateDiff (begindate,enddate): forma        T= "%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 #获取两个时间段的所有时间, returns list D        EF 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 I N Range (0,num): Li.append (Datetostr (ed)) Ed=ed-oneday return Li #获取当前年份 is a string def getye AR (): Return str (Datetime.date.today ()) [0:4] #获取当前月份 is a string def getMonth (): Return str (datetime.date.tod  Ay ()) [5:7] #获取当前天 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 (' 2008-10-01 ', ' 2008-10-05 ') print ' 2008-10-04 00:00:00 ' [0:10] Print str (getYear ()) +getmonth () +getday () print Getnow ()
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.