Python Learning--datetime Module

Source: Internet
Author: User

import datetime, Calendardate = Datetime.datetime.now () Gets the current system time #1, return to yesterday's datedef getyesterday ():Today=datetime.date.today ()Oneday=datetime.timedelta (Days=1)Yesterday=today-onedayreturn yesterday #2, return to today's datedef gettoday ():return Datetime.date.today () #3, gets the date of the previous day of the given argument, returns a listdef getdaysbynum (num):Today=datetime.date.today ()Oneday=datetime.timedelta (Days=1)li=[]For i in range (0,num):#今天减一天, day by day minusToday=today-oneday#把日期转换成字符串#result =datetostr (today)Li.append (Datetostr (today))return Li #4, converting a string to a datetime typedef strtodatetime (datestr,format):return Datetime.datetime.strptime (Datestr,format) #5, time converted to string, formatted as 2008-08-02def datetostr (date):return str (date) [0:10] #6, two days apart, for example: 2008-10-03 and 2008-10-01 are two days apartdef datediff (begindate,enddate):format= "%y-%m-%d"; bd=strtodatetime (Begindate,format)ed=strtodatetime (Enddate,format)Oneday=datetime.timedelta (Days=1)count=0While bd!=ed:Ed=ed-onedaycount+=1return Count #7, gets all the time of two time periods, returns a listdef getDays (begindate,enddate):format= "%y-%m-%d"; bd=strtodatetime (Begindate,format)ed=strtodatetime (Enddate,format)Oneday=datetime.timedelta (Days=1)Num=datediff (begindate,enddate) +1li=[]For i in range (0,num):Li.append (Datetostr (ed))Ed=ed-onedayreturn Li #8, gets the current year is a stringdef getYear ():return str (datetime.date.today ()) [0:4] #9, gets the current month is a stringdef getMonth ():return str (datetime.date.today ()) [5:7] #10, gets the current day is a stringdef 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 () #11, format the string as time Import datetime>>> s= "2006-1-2"Print Datetime.datetime.strptime (S, "%y-%m-%d")2006-01-02 00:00:00Import Time>>> s= "2006-1-2">>> Time.strptime (S, "%y-%m-%d")>>> from time import *>>> strftime ("%y-%m-%d%h:%m:%s", LocalTime ())' 2011-10-12 03:00:58 '#12, converting a format string to a timestamp>>> a = "Sat Mar 22:24:24">>> B = mktime (Strptime (A, "%a%b%d%h:%m:%s%Y"))>>> Print B1238250264.0>>>#13, datetime example-----------------demo Calculation of two date difference days>>> Import datetime>>> D1 = Datetime.datetime (2005, 2, +)>>> D2 = Datetime.datetime (2004, +)>>> (D1-D2). days -Shows examples of calculation run times, displayed in secondsImport datetimestarttime = Datetime.datetime.now ()#long Runningendtime = Datetime.datetime.now ()print (endtime-starttime). Seconds #14, Python date of the previous few days>>> from datetime import Timedelta, date>>> Print date.today () + timedelta (days =-2) (not a bit like date-d)2011-10-09>>> demonstrates the time to calculate the current time backwards by 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 the specific values.  #15, depending on the number of days to start, returns a list of dates relative to today such as mydate (0). Getdaysbynum (1, 7) will get a list of dates from the week beginning yesterday.  class MyDate: def __init__ (self, i):self.i = i def getdaysbynum (self, St, en):today = Datetime.date.today () + Datetime.timedelta (-SELF.I)oneday = Datetime.timedelta (Days=1)Global Yesterdayyesterday = Today-onedayli = []for I in range (0, en):today = Today-onedayli.append (str (today). Replace ("-", ""))return Li[st-1:en] #16, Glob: You can use simple methods to match all subdirectories or files in a directory, and the usage is simple. Glob.glob (regression) returns a listGlob.iglob (regression) returns a walker

Python Learning--datetime Module

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.