Python Time Operation example and time formatting parameter Summary

Source: Internet
Author: User
Tags month name

1. Method for retrieving the specific time in the past:
Copy codeThe Code is as follows:
#! /Usr/bin/python

Import time
# Obtain the current time of the previous day
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time ()-24*60*60 ))

# Obtain the current time 15 days ago
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time ()-15*24*60*60 ))

# The first two hours of the current time 15 days ago
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time ()-15*24*60*60-2*60*60 ))

2. method for getting the specific time in the future:
Copy codeThe Code is as follows:
#! /Usr/bin/python
Import time

# Obtain the current specific time after one day
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time () + 24*60*60 ))

# Obtain the current time in 20 days
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time () + 20*24*60*60 ))

# Take the first two hours of the current time in 20 days
Time. strftime ('% Y-% m-% d % t', time. localtime (time. time () + 20*24*60*60-2*60*60 ))

3. Take only the past and future values of a certain time unit:
Copy codeThe Code is as follows:
#! /Usr/bin/python
Import time

# Last month
Time. localtime () [1]-1

# Last Year
Time. localtime () [0]-1

# Take the month after two months
Time. localtime () [1] + 2

Time. strftime () format command:
Copy codeThe Code is as follows:
% A abbreviation of the day of the week
% A full name of the day of the week
Abbreviation of % B month
% B full name
% C standard date time string
The last two digits of the Year % C
% D indicates the day of the month in decimal format.
% D month/day/year
% E indicates the day of the month in decimal format in the two-character field
% F-month-day
The last two digits of the Year % g. The year is based on the week.
% G year, based on the week Year
% H abbreviated month name
% H in 24-hour format
% I 12-hour
% J indicates the day of the year in decimal format.
Month in % m decimal format
% M decimal number of minutes
% N new line operator
% P equivalent display of local AM or PM
% R 12 hours
% R display hour and minute: hh: mm
% S decimal seconds
% T horizontal Tab
% T display time in seconds: hh: mm: ss
% U the day of the week, Monday is the first day (value ranges from 0 to 6, Monday is 0)
% U indicates the week of the year. Sunday is regarded as the first day (the value ranges from 0 to 53)
% V indicates the week of the year, which is based on the year of the week.
% W decimal indicates the day of the week (the value ranges from 0 to 6, and Sunday is 0)
% W indicates the week of the year. Monday is the first day (from 0 to 53)
% X standard date string
% X standard time string
% Y does not contain the century's decimal year (the value ranges from 0 to 99)
% Y indicates the tenth year of the century.
% Z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned.
% Percent sign

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.