Python learning process (11) Date and time

Source: Internet
Author: User
Tags local time month name time and date

This section describes how the Python application processes time and date. The conversion date format is the most commonly used feature.

(1) Get timestamp:

Python provides a time and calendar module that can be used to format dates and times.

The function time.time () is used to get the current timestamp:

Import time >>> tickets=time.time ()print" current timestamp is:"  1469240559.05

Attention:

1. The time interval is a floating-point decimal in seconds.

2. Each timestamp is indicated by how long it has been since midnight January 1, 1970 (calendar).

3. Timestamps can only be displayed from 1970 to 2038.

(2) Time tuple and get current time:

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

This meta-ancestor is a struct_time tuple:

Get the current time and convert to a time tuple:

Import time >>> tickets=time.time ()print" current timestamp is "  1469241472.95>>> localtime=time.localtime (tickets)print" local time  ", LocalTime local time time.struct_time (tm_year=2016, tm_mon=7, tm_mday=23, tm_hour=10, tm_min =37, tm_sec=52, tm_wday=5, tm_yday=205, tm_isdst=0)

(3) Format time:

1) Get the formatted time directly:

>>> formtime=time.asctime (Time.localtime (Time.time ()))print" local time is: "  23 10:48:42 2016

2) arbitrarily get the formatted time:

>>>Import Time>>> localtime=Time.localtime (Time.time ())
#1. Format to date and time>>>PrintTime.strftime ("%y-%m-%d%h-%m-%s", localtime)2016-07-23 10-52-31
#2. Format to the day of the week month >>> minutePrintTime.strftime ("%a%b%d%h:%m:%s%Y", Time.localtime ()) Sat Jul10:55:59 2016>>>PrintTime.strftime ("%a%b%d%h:%m:%s%Y", LocalTime) Sat Jul23 10:52:31 2016
#3. Change the formatted time tuple back to a timestamp >>>PrintTime.mktime (Time.strptime (Time.strftime ("%a%b%d%h:%m:%s%Y"),"%a%b%d%h:%m:%s%Y"))1469242747.0

Python format symbols in time Date:

%y Two-digit year representation (00-99)%Y Four-digit year representation (000-9999)%m Month (01-12)One day in%d months (0-31)%H 24-hour hours (0-23)%I 12-hour hours (01-12)%M minutes (00=59)%s seconds (00-59)%a local simplified week name%A Local Full week name%b Local simplified month name%B Local Full month name%c Local corresponding date representation and time representation%j Day of the Year (001-366)%p equivalent of local a.m. or P.M.%u days of the year (00-53) Sunday for the beginning of the week%w Week (0-6), Sunday for the beginning of the week%W number of weeks in the year (00-53) Monday for the beginning of the week%x Local corresponding date representation%X Local corresponding time representation%Z name of the current time zonePercent% of the number itself

(4) 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:

Import Calendar>>> cal=calendar.month (2016,10)print  cal    Mo Tu We  Th Fr Sa Su                1  2 3  4  5  6  7  8  910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 3031

The time module and the date module also have many built-in functions for processing time and conversion time, which are not described here.

Python learning process (11) Date and time

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.