python's time, datetime and calendar

The datetime module is mainly used to represent the date, which is what we often call the year, month, day, hour, minute, and second. The calendar module is mainly used to represent the year, month, and day, and information such as the day of the week. The time module mainly focuses on hours, minutes, seconds, from From a simple point of view, we can think that the three are a complementary relationship. We can choose appropriate modules according to different purposes.


1.time.time() returns the current timestamp;

print (time.time()) #Output : 1525410174.8452504


2.time.ctime() returns the time character 'Wed Jun 8 15:27:48 2016' in this format, showing the current time;

print (time.ctime()) #Output : Fri May 4 13:02:54 2018


3.time.gmtime converts the timestamp into struct_time format, which displays the time in Greenwich 0 time zone;

print(time.gmtime())#输出:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=5, tm_min=2, tm_sec=54, tm_wday=4, tm_yday=124, tm_isdst=0)

 

4.time.localtime converts the current system timestamp into struct_time format;

print(time.localtime())#输出:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=13, tm_min=2, tm_sec=54, tm_wday=4, tm_yday=124, tm_isdst=0)


Note: The elements in the struct_time tuple mainly include tm_year (year), tm_mon (month), tm_mday (day), tm_hour (hour), tm_min (minute), tm_sec (second), tm_wday (weekday0 - 6 (0 means
Sunday) ), tm_yday (days of the year 1 - 366), tm_isdst (whether it is daylight saving time)

5.time.mktime Convert the struct_time format back to a timestamp;

print(time.mktime(time.localtime()))#输出:1525410174.0


6.time.strftime converts the struct_time format into the specified string format;

print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))#输出:2018-05-04 13:02:54


7.time.strptime converts the string of custom time format to struct_time format;

print(time.strptime("2018-05-04","%Y-%m-%d"))#输出:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4,tm_yday=124, tm_isdst=-1)


8.time.sleep Pause time.

print(time.sleep(2))


Note: If the two functions time.gmtime() and time.localtime() are called without parameters, they will internally call time.time() and use the returned seconds for conversion.

**************************************************** ********************************************

import datetime

1.datetime.datetime.today( ) Returns the object of the current date and time by default, or you can customize the date and time;

print (datetime.datetime.today()) #Output : 2018-05-04 13:15:57.819725 
print (datetime.datetime(2018,5,4,0,0,0)) #Note that the date here can only be is the actual month, cannot take 0 
#Output : 2018-05-04 00:00:00


2. datetime.datetime.now() returns the current time;

print(datetime.datetime.now())#输出:2018-05-04 13:15:57.819725


3.datetime.strftime(format) #custom format time;

nowtime = datetime.datetime.now()
 print (nowtime.strftime( " %I:%M:%S %p %d/%m/%Y " )) #custom format time 
#output : 01:15 : 57 PM 04/05/2018


4. datetime.datetime.timple() converts time to struct_time format;

print(nowtime.timetuple())#输出:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=13, tm_min=15, tm_sec=57, tm_wday=4, tm_yday=124, tm_isdst=-1)


5. Datetime time operation:

nowtime =datetime.datetime.now() #The current time is: 2018-05-04 13:15:57.819725 
print (nowtime-datetime.timedelta(days=1)) #Output : 2018-05-03 13:15:57.819725 
print (nowtime-datetime.timedelta(hours=3)) #Output : 2018-05-04 10:15:57.819725



**************************************************** ****************************************

import calendar

1. Go back to the calendar

print (calendar.month(2018,5 ))
 #Output : 
      May 2018
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325250826&siteId=291194637