Python Date and Time

Source: Internet
Author: User
Tags month name timedelta

Whenever and wherever we encounter time-related problems during programming, we need to think of the datetime and time standard library modules. Today we will use its internal methods, describes the date and time operations in python.
1. Convert the string time to the timestamp
Copy codeThe Code is as follows:
A = "23:40:00"
# Convert it to a time array
Import time
TimeArray = time. strptime (a, "% Y-% m-% d % H: % M: % S ")
# Convert to timestamp:
TimeStamp = int (time. mktime (timeArray ))
TimeStamp = 1381419600
2. format change
For example, to change a = "23:40:00" to a = "23:40:00"
Method: first convert to a time array, and then convert to another format
Copy codeThe Code is as follows: timeArray = time. strptime (a, "% Y-% m-% d % H: % M: % S ")
OtherStyleTime = time. strftime ("% Y/% m/% d % H: % M: % S", timeArray)
3. Convert the timestamp to a date in the specified format
Method 1: Use localtime () to convert it to a time array and format it as needed, for example:
Copy codeThe Code is as follows: timeStamp = 1381419600
TimeArray = time. localtime (timeStamp)
OtherStyleTime = time. strftime ("% Y-% m-% d % H: % M: % S", timeArray)
OtherStyletime = "23:40:00"
Method 2:
Copy codeThe Code is as follows: import datetime
Time stamp = 1381419600
DateArray = datetime. datetime. utcfromtimestamp (timeStamp)
OtherStyleTime = dateArray. strftime ("% Y-% m-% d % H: % M: % S ")
OtherStyletime = "23:40:00"
4. Get the current time and convert it to the specified date format
Method 1:
Copy codeThe Code is as follows: import time
# Obtain the current Timestamp
Now = int (time. time ()-> This is the timestamp
# Convert to another date format, for example, "% Y-% m-% d % H: % M: % S"
TimeArray = time. localtime (timeStamp)
OtherStyleTime = time. strftime ("% Y-% m-% d % H: % M: % S", timeArray)

Method 2:
Copy codeThe Code is as follows: import datetime
# Obtain the current time
Now = datetime. datetime. now ()-> This is the time array format
# Convert to the specified format:
OtherStyleTime = now. strftime ("% Y-% m-% d % H: % M: % S ")
5. Obtain the time three days ago
Copy codeThe Code is as follows: import time
Import datetime
# First obtain the date in the time array format
ThreeDayAgo = (datetime. datetime. now ()-datetime. timedelta (days = 3 ))
# Convert to timestamp:
TimeStamp = int (time. mktime (threeDayAgo. timetuple ()))
# Convert to other string formats:
OtherStyleTime = threeDayAgo. strftime ("% Y-% m-% d % H: % M: % S ")
Note: timedelta () parameters include: days, hours, seconds, and microseconds.
6. Calculate the timestamp several days ago
Copy codeThe Code is as follows: timeStamp = 1381419600
# Convert to datetime first
Import datetime
Import time
DateArray = datetime. datetime. utcfromtimestamp (timeStamp)
ThreeDayAgo = dateArray-datetime. timedelta (days = 3)
# Refer to 5, which can be converted to any other format
7. Use Python to calculate the date of yesterday and tomorrow
Copy codeThe Code is as follows: >>> import datetime # import date and time module
>>> Today = datetime. date. today () # obtain the date of today
>>> Print today # output today's date
2014-01-04
>>> Yesterday = today-datetime. timedelta (days = 1) # subtract the time difference from the date of today. The parameter is 1 day and the date of yesterday is obtained.
>>> Print yesterday
2014-01-0
>>> Tomorrow = today + datetime. timedelta (days = 1) # use today's date plus time difference. The parameter is 1 day to get tomorrow's date
>>> Print tomorrow
2014-01-05
>>>
>>> Print "yesterday: % s, today: % s, tomorrow: % s" % (yesterday, today, tomorrow) # string concatenated and output, the date of these three days
Yesterday:, today:, tomorrow:
8. Use the time module in python to obtain the current time.
Copy codeThe Code is as follows:
#! /Usr/bin/python
Import time
Print (time. strftime ("% H: % M: % S "))
#12 hour format ##
Print (time. strftime ("% I: % M: % S "))
#: Output
#18:11:30
#6:11:30

9. The python program that prints the current date
Copy codeThe Code is as follows :! /Usr/bin/python

Import time
# Dd/mm/yyyy format
Print (time. strftime ("% d/% m/% Y "))

# Output:
11/03/2014

10. Use the datetime module to obtain the current date and time.
Copy codeThe Code is as follows :#! /Usr/bin/python
Import datetime
I = datetime. datetime. now ()
Print ("current date and time is % s" % I)
Print ("ISO format date and time is % s" % I. isoformat ())
Print ("the current year is % s" % I. year)
Print ("the current month is % s" % I. month)
Print ("the current date is % s" % I. day)
Print ("dd/mm/yyyy format is % s/% s" % (I. day, I. month, I. year ))
Print ("current hour is % s" % I. hour)
Print ("Current minute is % s" % I. minute)
Print ("current second is % s" % I. second)

Appendix: format parameters of date and time
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.