Python's built-in modules time and DateTime methods are explained in detail and used (in Python, in both temporal and datetime formats)

Source: Internet
Author: User
Tags local time locale month name time and seconds

Methods for time built-in modules

1, Time () timestamp

Time (), floating point number float
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.

Import  Time Print (Time.time ()) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. PY1528517838.7509072

This timestamp is a number of seconds, starting from the wee hours of 1970, and how many seconds it's been.

It's 2018, minus 1970, it's 48,

48*365*24*60*60=1513728000

haha calculate the same as above, this is the time stamp, every second is not the same

Timestamps can be used to calculate the 2-time subtraction, that is, when I order the time is a timestamp, I pay a successful one time stamp, you can calculate how many seconds I have to pay the order

2, localtime (seconds=None) structured time-local time
Get a structured time

Convert seconds since the Epoch to a time tuple expressing local time.
When ' seconds ' isn't passed in, convert the current time instead

Import  Time Print (Time.localtime ()) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. Pytime.struct_time (tm_year =2018, tm_mon=6, tm_mday=9, tm_hour=12, tm_min=36, tm_sec=7, tm_wday=5, tm_yday=160, tm_isdst=0)

Then we can take it out. Specific years or seconds, the day of the week, the day ordinal of a year

Import Timea=time.localtime ()print(A.tm_year,a.tm_mon,a.tm_mday,a.tm_hour,":  ", A.tm_min,":", A.tm_sec) C:\python35\python3.exe D:/ Pyproject/day21 module/Timemodule. PY2018 6 9 12:46:1
3, Gmtime is also a structured time of the world standardization time-UTC

Time Standard Time, 8 hours with our difference

4, Mktime (p_tuple)

Mktime (tuple), floating point number

Convert structured time to timestamp

Import  Time Print (Time.mktime (Time.localtime ())) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. PY 1528522939.0

5, strftime (format, p_tuple=None)

Convert structured time to string time

%Y year with century as a decimal number.%m Month as a decimal number [01,12].%d day of the month as a decimal number [01,31].%H Hour (24-hour clock) as a decimal number [00,23].%M Minute as a decimal number [00,59].%s Second as a decimal number [00,61].%z Time zone offset fromUTC.%a Locale's abbreviated weekday name.%A Locale's full weekday name.%b Locale's abbreviated month name.%B Locale's full month name.%c Locale's appropriate date and time representation.%I Hour (12-hour clock) as a decimal number [01,12].%p Locale's equivalent of either AM or PM.

%x on behalf of time and seconds

Import  Time Print (Time.strftime ("%y-%m-%d%x", Time.localtime ())) C:\python35\python3.exe D:/ Pyproject/day21 module/Timemodule. PY2018-06-09 15:11:04
6, Strptime (string, format)

Convert string time to structured time

%Y year with century as a decimal number.%m Month as a decimal number [01,12].%d day of the month as a decimal number [01,31].%H Hour (24-hour clock) as a decimal number [00,23].%M Minute as a decimal number [00,59].%s Second as a decimal number [00,61].%z Time zone offset fromUTC.%a Locale's abbreviated weekday name.%A Locale's full weekday name.%b Locale's abbreviated month name.%B Locale's full month name.%c Locale's appropriate date and time representation.%I Hour (12-hour clock) as a decimal number [01,12].%p Locale's equivalent of either AM or PM.

The string time here corresponds to the structure time one by one behind it.

Import  Time Print (Time.strptime ("2018:06:09-15:21:36","%y:%m:%d-%x")) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. Pytime.struct_time (tm_year=2018, Tm_mon=6, TM _mday=9, tm_hour=15, tm_min=21, tm_sec=36, tm_wday=5, tm_yday=160, Tm_isdst=-1)
7, Asctime (p_tuple=None) can be added to the structure of parameters, no parameter default is the current time

If you don't have a custom demand time format, you can use this method

Convert a time tuple to a string, e.g. ' Sat June 06 16:26:11 1998 '.
When the time of a tuple is not present, current time as returned by LocalTime ()
is used

Import  Time Print (Time.asctime ()) C:\python35\python3.exe D:/pyproject/day21 module/Timemodule. Pysat June  9 15:26:45 2018
8, CTime (seconds=None) can be added timestamp parameter, no parameter default is the current time

It's the same format as the 7 asctime.

Print (Time.ctime ()) # without arguments, the default is the current time Sat June   9 15:34:30 2018Print(Time.ctime (1228629586.2798274))# plus timestamp (string time) parameter Sun Dec   7 13:59:46 2008

9, DateTime This relatively easy to use, the first usage is relatively fine, the second format is more beautiful

Import datetime Print (Datetime.datetime.now ()) Print (Datetime.datetime.now (). Strftime ("%y-%m-%d%x")) C:\python35\python3.exe D:/ Pyproject/day21 module/Timemodule. py2018-06-09 15:44:29.8709262018-06-09 15:44:29

Python's built-in modules time and DateTime methods are explained in detail and used (in Python, in both temporal and datetime formats)

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.