Basic date-time processing in Python learning tutorial

Source: Internet
Author: User
Python programs can handle dates and times in many ways. Converting a date format is a common routine chore. Python has a time and calendar module to help.
What is tick?
The time interval is a floating-point decimal in seconds.
Each timestamp is represented by how long it has been since midnight on January 1, 1970 (the calendar).
There are many functions under the popular time module included with Python to convert common date formats. such as the function Time.time () returns the current operating system time of the record starting from 12:00am, January 1, 1970 (epoch) with the Ticks Timing unit, as in the following example:

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

The result of the above example output:

Number of ticks since 12:00am, January 1, 1970:7,186,862,.73,399


Tick units are best suited for date operations. But the dates before 1970 cannot be expressed in this way. Too far away from the date, UNIX and Windows only support the day of 2038.

What is a time tuple?
Many Python functions use a single element to assemble 9 sets of digital processing time:

The above is also the struct_time tuple. This structure has the following properties:

Get current time
Converts from the time of the return floating-point number to the time tuple, as long as the floating-point number is passed to a function such as localtime.

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

The result of the above example output:

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 formatted time
You can choose a variety of formats according to your needs, but the simplest function to get a readable time pattern is asctime ():

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

The result of the above example output:

Local current Time:tue Jan 13 10:17:09 2009


Get a calendar for a month
The Calendar module has a wide range of methods for working with calendar and monthly calendars, such as printing a month's monthly calendar:

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

The result of the above example output:

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 2 4 25 26 2728 29 30 31
  • 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.