Python Beginner (vi)

Source: Internet
Author: User
Tags local time time and date timedelta

XV, Time,datetime,random module

I. Time & DateTime

These two modules can handle time and date;

Time modules are commonly used in the following ways: Time (), Process_time () (Clock ()), Altzone,asctime (), localtime (), Gmtime (), CTime (), Strptime (), Mktime () , Strftime ();

The common methods of datetime modules are: DateTime.Now (), Date.fromtimestamp (), Timedelta ().

>>>ImportTime 
>>> time.time () # returns the timestamp format time of the current time (1970 to the current number of floating-point seconds)
1473691580.9504104
>>> Time.process_time ()# Measure Processor Time timestamp format time, not including sleep time, clock () method returns processor Time
0.49920319999999996
>>> Time.altzone# returns time stamp format time with UTC time (Universal time coordinated, Internet rescue standard times)
-32400
>>> Time.gmtime (Time.time ())# convert UTC timestamp to struct_time format
Time.struct_time (tm_year=2016, tm_mon=9, tm_mday=13, tm_hour=14, tm_min=59, tm_sec=19, Tm_wday=1, tm_yday=257, tm_ isdst=0)
>>> Time.localtime ()#for output local time struct_time formatTime.struct_time (tm_year=2016, tm_mon=9, tm_mday=4, tm_hour=17, tm_min=14, tm_sec=28, tm_wday=6, tm_yday=248, TM_ISDST =R)>>> Time.asctime ()#Output formatted time' Sun Sep 4 17:15:30 '>>>Time.asctime (Time.localtime ())' Sun Sep 4 17:17:55 '
>>> time.ctime () # with Asctime ()
' Tue Sep 23:10:16 ' >>> time.strptime (' 20160904 ', '%y%m%d ')#The preceding string and subsequent formats require a specific formatTime.struct_time (tm_year=2016, tm_mon=9, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=248, Tm_isdst=-1)
>>> time.strftime ('%y-%m-%d%h:%m:%s ', Time.gmtime ()) # Convert the struct_time time format to a time string in the specified format
' 2016-09-13 15:12:40 '
>>>Importdatetime>>> Datetime.datetime.now ()#Output Current TimeDatetime.datetime (2016, 9, 4, 17, 27, 28, 482526)>>> Datetime.date.fromtimestamp (Time.time ())#convert timestamps to date formatsDatetime.date (2016, 9, 4)>>> Datetime.datetime.now () + Datetime.timedelta (hours=3)#Timedelta () can be used for time addition and subtractionDatetime.datetime (2016, 9, 4, 20, 29, 28, 853891)

DateTime formatted string (other places below are transcribed and translated, not all in practice)

DateTime formatted string
%a Abbreviation of the Week
%A Full name of the week
%b Abbreviation of the Month
%B Full name of the month
%c Date and time representation
%d The day ordinal of a month (decimal)
%H Hours (decimal, 24-hour format)
%I Hours (decimal, 12-hour format)
%j The day ordinal of a year (decimal)
%m Month (decimal)
%M Minutes (decimal)
%s Seconds (decimal)
%u The week of the Year (decimal, Sunday is the first day of the week) (the first Sunday of the new Year is considered the beginning of the No. 0 week)
%w Day of the week (Sunday for 0)
%W The week of the Year (decimal, Monday is the first day of the week) (the first Monday of the new Year is considered the beginning of the No. 0 week)
%x Date representation
%x Time representation
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no strings exist without time zone)
%z Time zone offset indicating a positive or negative time difference from utc/gmt of the form +hhmm or-hhmm, where H repres Ents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%% Percent percent "%"

II. Random

Used to generate random numbers, the commonly used methods are:

Random () (produces a randomly fractional number between 0-1);

Randint (A, B) (generates a random number between-A and contains a, both of which are integers);

Randrange (A, B) (produces a random number between a-B, which consists of the two parameters integer).

Python Beginner (vi)

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.