Python time manipulation examples and time formatting parameters summary

Source: Internet
Author: User
1. How to take the time of the past:

#!/usr/bin/python  Import Time  #取一天前的当前具体时间   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time ()-24 *60*60))  #取15天前的当前具体时间   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time () -15*24*60*60))  # Take 15 days before the current specific time of the first 2 hours   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time () -15*24*60*60-2*60*60))

2. How to take the specific time in the future:

#!/usr/bin/python   Import Time  #取一天后的当前具体时间   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time () +24 *60*60))   #取20天后的当前具体时间   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time () +20*24*60*60))   #   time.strftime ('%y-%m-%d%T ', Time.localtime (Time.time () +20*24*60*60-2*60*60) for the first 2 hours of the current specific time after 20 days

3. Take only a certain time unit past, future specific values:

#!/usr/bin/python   Import Time  #取上一月月份   time.localtime () [1]-1   #取去年年份   time.localtime () [0]-1   #取两个月后的月份   time.localtime () [1]+2

Time.strftime () Format command:

%a Day of the week%a the full name of the day of the   week   %b Month of the abbreviated%b month full   name   %c Standard date of the time string   %c year of the last two digits   %d decimal representation of the month of the first few   %d Month/day/year   %e in two-character fields, decimal represents the number of days of the month   %F year-month-day   %g year of the last two digits, using the week-based year   %g years, using the month name based on the week's   %h shorthand   %h Hour%I 24-hour system   The 12-hour hour   %j decimal represents the number of minutes of the day of the year%m decimal represents the minute of the   month   %m the   %n New line character   %p local AM or PM equivalent display   %r 12 hours of time   %R display hours and minutes: hh:mm   %s decimal seconds   %t horizontal tab   %t display seconds: Hh:mm:ss   %u the day of the week, Monday for first days (values from 0 to 6, Monday to 0)   %u year of the week, the Sunday as the first day (value from 0 to%V) the week of   the year, using a week-based   %w decimal Day (value from 0 to 6, Sunday is 0)   %w the week of the year, Set Monday as the first day (value from 0 to max)   %x standard   time string   %y the decimal Year without the Century (values from 0 to%y) The   10 year   %z,%z time zone name with the century part, Returns a null character if the time zone name cannot be obtained. Percent   hundred percent semicolon

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.