Python time module datetime, time, calendar usage, pythondatetime

Source: Internet
Author: User
Tags month name timedelta

Python time module datetime, time, calendar usage, pythondatetime

This article briefly summarizes the Python processing time and date modules, mainly the use of datetime, time, and calendar modules. I hope this article will be helpful to anyone who has learned Python.

The first is the module call. Many ides have installed many modules that are frequently used by Python. Therefore, we do not need to install the modules for the time being.

import datetimeimport timeimport calendar

1. Get the accurate time at this time

# Obtain the current time print time. localtime ()
// The output format is time. struct_time (tm_year = 2015, tm_mon = 12, tm_mday = 29, tm_hour = 1, tm_min = 10, tm_sec = 25, tm_wday = 1, tm_yday = 363, tm_isdst = 0)

2. Get the date of the current day

# Obtain the date print datetime. datetime. now () print datetime. date. today () of the current day ()

3. Get the date of yesterday

# Retrieve the date def getYesterday (): today = datetime. date. today () oneday = datetime. timedelta (days = 1) yesterday = today-oneday print type (today) # view the obtained time type print type (yesterday) return yesterdayyesterday = getYesterday () print "yesterday's time: ", yesterday

4. Get the date n days ago
You don't need to provide the code. You can get the result by thinking about it.

5. String Conversion to time and date

# String Conversion to time def strTodatetime (datestr, format): return datetime. datetime. strptime (datestr, format) print time. strftime ("% Y-% m-% d", time. localtime () print strTodatetime ("2014-3-1", "% Y-% m-% d") print time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime () print strTodatetime ("2005-2-16", "% Y-% m-% d")-strTodatetime ("2004-12-31", "% Y-% m-% d ")

Output result:
Septem
00:00:00
01:10:25
47 days, 0:00:00
6. Obtain calendar-related information

# Obtain the calendar of a month. The string type cal = calendar is returned. month (2015, 12) print calcalendar. setfirstweekday (calendar. SUNDAY) # set the first day of the calendar cal = calendar. month (2015, 12) print cal # obtain the calendar cal of a year = calendar ar. calendar (2015) print calcal = calendar. HTMLCalendar (calendar. MONDAY) print cal. formatmonth (2015, 12)

7. The calendar module can also handle the leap year problem.

# Determine whether a leap year is used. print calendar. isleap (2012) print calendar. leapdays (2010,201 5)

The datetime \ time module of Python is discussed in detail.

The meanings of escape characters are as follows:

  • % A local simplified week name
  • % A local full week name
  • % B local simplified month name
  • % B local full month name
  • % C local Date and Time
  • One day in % d month (0-31)
  • % H hour in 24-hour format (0-23)
  • % I 12-hour (01-12)
  • % J one day in the year (001-366)
  • % M month (01-12)
  • % M minutes (00 = 59)
  • % P local equivalent of A. M. or P. M.
  • % S seconds (00-59)
  • % U number of weeks in a year (00-53) Sunday is the start of the week
  • % W Week (0-6), Sunday is the beginning of the week
  • % W number of weeks in a year (00-53) Monday is the start of the week
  • % X local date Representation
  • % X Local Time Representation
  • % Y two-digit year (00-99)
  • % Y indicates the four-digit year (000-9999)
  • % Z Current Time Zone name
  • % Itself

Code:

Import time import datetime # two dates minus d1 = datetime. datetime (2005, 2, 16) d2 = datetime. datetime (2004, 12, 31) print (d1-d2 ). days # Run Time: starttime = datetime. datetime. now () endtime = datetime. datetime. now () print (endtime-starttime ). seconds # calculates the time from the current time to the next 10 days. # Replace hour days with hours d1 = datetime. datetime. now () d3 = d1 datetime. timedelta (days = 10) print str (d3) print d3.ctime () time. ctime ([sec]) # converts the number of seconds to the date format. If no parameter is specified, the current time is displayed. >>> Import time >>> time. ctime () >>> "Wed Jun 14 15:02:50 2006" >>> time. ctime (1138068452427683) "Sat Dec 14 04:51:44 1901" >>> import time >>> time. strftime ("% Y-% m-% d % X", time. localtime () "20:42:12"> time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime () "20:03:47" DateTime module ------------------------------ datetime converts the date to seconds >>> import datetime, time >>> time. mktime (datetime. datetime (2009,1, 1 ). timetuple () 1230739200.0 >>> cc = [, 11,] # Attributes: year, month, day, hour, minute, second >>> time. mktime (datetime. datetime (cc [0], cc [1], cc [2], cc [3], cc [4], cc [5]). timetuple () 973226613.0time.time () gets the current time; time. localtime () gets the local time; time. strftime () format the date; time. strptime (timeString) converts a string to a date, and judges the input date as the day of the week >>> datetime. datetime (2011,02, 15 ). weekday () 1 >>> datetime. datetime (2011,02, 15 ). weekday () 1 >>> datetime. datetime (2011,02, 16 ). weekday () 2 >>> datetime. datetime (2011,02, 17 ). weekday () 3 >>> datetime module obtains the current time >>> datetime. datetime. utcnow () datetime. datetime (2011, 3, 15, 13, 19, 32,264 194) >>> datetime. datetime. utcnow (). strftime ("% Y-% m-% d % H: % M: % S") format '2017-03-15 13:19:27 '>>>

The above is a detailed study of the Python time module, hoping to help you learn Python programming.

Articles you may be interested in:
  • Python time processing datetime instance
  • Make python json encode datetime type
  • Introduction to datetime in Python
  • Remove leading 0 from Python datetime time formatting
  • Python uses the datetime module to calculate various time intervals
  • Conversion between Timestamp and Datetime and UTC time in Python
  • Python uses the datetime module to calculate the time difference
  • The difference between the time module and the datetime module in Python
  • Description of the datetime module in the python time module

Related Article

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.