Python basic Date and Time Processing tutorial, python tutorial

Source: Internet
Author: User

Python basic Date and Time Processing tutorial, python tutorial

Python programs can process dates and times in many ways. Converting the date format is a common task. Python has a time and calendar module for help.
What is Tick?
The interval is a floating point decimal point in seconds.
Each timestamp is represented by the time elapsed since midnight, January 1, January 1, 1970.
Many functions in the popular time module that comes with Python can convert common date formats. For example, the time. time () function returns the current operating system time of the record starting from am, January 1, 1970 (epoch) with the ticks timer unit, as shown in the following example:

#!/usr/bin/pythonimport time; # This is required to include time module.ticks = time.time()print "Number of ticks since 12:00am, January 1, 1970:", ticks

Output result of the above instance:

Number of ticks since 12:00am, January 1, 1970: 7186862.73399


The Tick unit is most suitable for date calculation. However, the date before January 1, 1970 cannot be expressed as this. The date is too far away, and UNIX and Windows only support the date of January 1, 2038.

What is a time tuples?
The processing time for many Python functions to assemble 9 sets of data with one element:

The above is the _time tuples. This structure has the following attributes:

Get current time
If you want to convert the time taken from the returned floating point to the time tuples, you only need to pass the floating point to a function such as localtime.

#!/usr/bin/pythonimport time;localtime = time.localtime(time.time())print "Local current time :", localtime

Output result of the above instance:

Local current time : time.struct_time(tm_year=2013, tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)


Get the formatting time
You can select various formats as needed, but the simplest function to obtain the readable time mode is asctime ():

#!/usr/bin/pythonimport time;localtime = time.asctime( time.localtime(time.time()) )print "Local current time :", localtime

Output result of the above instance:

Local current time : Tue Jan 13 10:17:09 2009


Get calendar of a month
The Calendar module has a wide range of methods for processing calendars and calendars, such as printing a Calendar month:

#!/usr/bin/pythonimport calendarcal = calendar.month(2008, 1)print "Here is the calendar:"print cal;

Output result of the above instance:

Here is the calendar:  January 2008Mo Tu We Th Fr Sa Su  1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31

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.