Python Date-time processing

Source: Internet
Author: User

# Get Date: Import datetime #调用事件模块today =datetime.date.today () #获取今天日期deltadays =datetime.timedelta (Days=1) #确定日期差额, as the day before yesterday Days=2yesterday =today-deltadays # Get difference date, yesterday tomorrow =today +dletadays # Get difference date, tomorrow # date format output isoformat= '%y%m%d ' # Set output format print today.strftime (isoformat) dt = DateTime.Now () print ' (%y-%m-%d%h:%m:%s%f): ', Dt.strftime ('%y-%m-%d%h:%m:%  S%f ') print ' (%y-%m-%d%h:%m:%s%p): ', Dt.strftime ('%y-%m-%d%i:%m:%s%p ') print '%%a:%s '% dt.strftime ('%a ') print '%%a:%s '% dt.strftime ('%A ') print '%%b:%s '% dt.strftime ('%b ') print '%%b:%s '% dt.strftime ('%b ') print ' Date time% C:%s '% dt.strftime ('%c ') print ' date%%x:%s '% dt.strftime ('%x ') print ' time%%x:%s '% dt.strftime ('%x ') print ' Today is this week's% S Day '% dt.strftime ('%w ') print ' Today is this year's%s day '% dt.strftime ('%j ') print ' This week is this year's%s week '% dt.strftime ('%u ') # #----Results   ----# (%y-%m-%d%h:%m:%s%f): 2010-04-07 10:52:18 937000 # (%y-%m-%d%h:%m:%s%p): 10-04-07 10:52:18 AM #%a:wed #%a:wednesday #%b:apr #%b:april   # datetime%C:04/07/10 10:52:18 # date%X:04/07/10 # time%x:10:52:18 # today is the 3rd day of the week # today is the No. 097 Day of the year # this week is the 14th week of this year gets , the day after the date datetime.date (2015,01,04) +datetime.timedelta (days=+1) datetime.date (2015,01,04) +datetime.timedelta (days= -1) #获取时间: Import time# Current time Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) #获取上周日日期isoformat = "%y%m%d" Lastsunday = Datetime.date.today ()-Datetime.timedelta (Days=datetime.date.today (). Isoweekday ()) Print lastsunday.strftime ( Isoformat) #获取上周日所有日期def Getweek (): week = {} Isoformat = "%y%m%d" Lastsunday = Datetime.date.today ()-DateTime.     Timedelta (Days=datetime.date.today (). Isoweekday ()) Sunday = Int (Lastsunday.strftime (isoformat)) week[' Sun ' = Sunday week[' Sat '] = sunday-1 week[' Fri '] = sunday-2 week[' Thur '] = sunday-3 week[' Wed '] = sunday-4 Week [' Tue '] = sunday-5 week[' Mon '] = sunday-6 return weekprint getweek () #当月第一天import time,datetimed_today = Datetim E.datetime.today () D_first = Datetime.datetime(D_today.year, D_today.month, 1) result = Time.mktime (D_first.timetuple ()) 

Python date-time processing

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.