Python Date and Time 2018-07-30

Source: Internet
Author: User
Tags month name

    1. Python provides a time and calendar module for formatting dates and times, the interval is a floating point number in seconds, each timestamp is represented by how long it has been since midnight of 1970 (the calendar Element) , and the timestamp unit is best suited for a date operation. But the dates before 1970 cannot be expressed in this way. Too distant a date, UNIX and Windows only support to 2038, because the 32-bit system represents the maximum positive number is 2147483647, the total seconds of 365 days a year is 31536000,2147483647/31536000=68.1, That is, 32 is the longest time to be represented is 68.1 years, so from 1970-2038
    2. Use time.time() to get the current timestamp
# -*- coding: UTF-8 -*-                                          import time;ticks = time.time()print "当前时间戳:",ticks

Output:

[email protected]:~/code$ python test.py 当前时间戳: 1532949645.19
    1. The Python function uses a single element to assemble 9 sets of digital processing time:
    2. Get current time: Convert to a time tuple from a timestamp that returns a floating-point number, as long as the floating-point number is passed to a function such as localtime
# -*- coding: UTF-8 -*-                                          import timelocaltime = time.localtime(time.time())print "本地时间:",localtime

Output:

[email protected]:~/code$ python test.py 本地时间: time.struct_time(tm_year=2018, tm_mon=7, tm_mday=30, tm_hour=19, tm_min=37, tm_sec=19, tm_wday=0, tm_yday=211, tm_isdst=0)
    1. Gets the formatted time: to select various formats according to the requirements, but the simplest function to get a readable time pattern is asctime ():
# -*- coding: UTF-8 -*-import timelocaltime = time.asctime(time.localtime(time.time()))print "本地时间:",localtime  

Output:

[email protected]:~/code$ python test.py 本地时间: Mon Jul 30 19:41:03 2018
    1. Formatted date
      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 a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself

# -*- coding: UTF-8 -*-                                          import timeprint time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())print time.strftime("%a %d %m %H:%M:%S %Y",time.localtime())# 将格式字符串转换为时间戳a = "Mon July 30 19:50:23 2018"print time.mktime(time.strptime(a,"%a %B %d %H:%M:%S %Y"))

Output:

[email protected]:~/code$ python test.py 2018-07-30 19:57:59Mon 30 07 19:57:59 20181532951423.0
    1. Get a calendar for a month
# -*- coding: UTF-8 -*-import calendarcal = calendar.month(2018,7)                                     print cal

Output:

[email protected]:~/code$ python test.py      July 2018Mo Tu We Th Fr Sa Su                   1 2  3  4  5  6  7  8 9 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31

Python Date and Time 2018-07-30

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.