Python Simple Time Date processing

Source: Internet
Author: User
Some features are necessary when using a programming language.

There is the calculation of the date.

Import datetime# Date initialization: D1 = Datetime.datetime (2005, 2, +) D2 = Datetime.datetime (2004, +) #日期相减: print (D1-D2). Dayssta Rttime = Datetime.datetime.now () Endtime = Datetime.datetime.now () print (endtime-starttime). Secondsstarttime = Datetime.datetime.now () starttime.year starttime.day starttime.second #上例演示了计算运行时间的例子, displayed in seconds. #使用timedelta进行加减: D1 = Datetime.datetime.now () #d3 = D1 + Datetime.timedelta (days =10) d3 = d1 + Datetime.timedelta (days =-1) Print str (d3) print d3.ctime () print d3.strftime ('%y-%m-%d '); #上例演示了计算当前时间向后10天的时间. #如果是小时 days replaced by hours# the classes used in this book are: DateTime and Timedelta two. They can be added and reduced between each other. #time, datetime, string types convert each other. String-Timetime.strptime (Publishdate, "%y-%m-%d%h:%m:%s") #2. String-Datetimedatetime.datetime.strptime (Publishdate, "%y-%m-%d%h:%m:%s") #3. Time-Stringtime.strftime ("%y-%m-%d", Time.localtime ()) date = ' 2007-01-01 ' t = time.strptime (date, "%y-%m-%d") # Struct_time Type D = Datetime.datetime (Date[0], date[1],date[2]) #datetime类型 # Converts a DateTime object to a string date = Time.strftime ("%y-%m-%d ", D) d.strftime ('%y%m%d ') #日期格式化: '%a week shorthand. As Wednesday for Web%a week's full write. For example, Wednesday is abbreviated for WEDNESDAY%B month. such as April for the full write of Apr%b month. As in April, april%c: A string representation of the DateTime. (Example: 04/07/10 10:43:39)%d: Days in this month (the day of the week)%f: microseconds (range [0,999999])%H: Hours (24-hour, [0,])%I: Hours (12-hour, [0, one])%j: Days in the year [ 001,366] (the day of the year)%m: Month ([01,12])%m: minutes ([00,59])%p:am or pm%s: seconds (range = [00,61], why not [00, 59], refer to Python manual ~_~)%u: Weeks in the current year of the Week of the year, Sunday as the first day of the week%w: Today in this week, the number of days, the range is [0, 6],6 represents Sunday%w: Week in the Year of the week (is the first week of the year), Monday as the day of the week%x: date string (such as: 04/07/10)%x: A time string (such as: 10:43:39)%y:2 A number representing the year%y:4 the year%Z: Interval from UTC (If local time, returns an empty string)%Z: Time zone name (returns an empty string if local time) percent:% =% ' dt = Datetime.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 ')
  • 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.