python module --time module

 

1. Timestamp

1  import time
 2  # ------->> timestamp 
3  print (time.time())
1 1525418832.2835853

2. Structured time

1  # ------->> Structured time 
2  print (time.localtime())
 3  print (time.gmtime())
 4  print (time.localtime().tm_mday)
1 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=15, tm_min=28, tm_sec=32, tm_wday=4, tm_yday=124, tm_isdst=0)
2 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=7, tm_min=28, tm_sec=32, tm_wday=4, tm_yday=124, tm_isdst=0)
3 4

3. Convert structured time to timestamp

1  # ------->> Convert structured time to timestamp 
2  print (time.mktime(time.localtime()))
1 1525418978.0

4. Convert structured time to string time

1  # ------->> Convert structured time to string time 
2  print ( time.strftime( " %Y-%m-%d %X " ,time.localtime()))   #The following Hours, minutes and seconds can be simplified with %X to write 
3  print ( time.strftime( " %Y-%m-%d %H:%M:%S " ,time.localtime()))
1 2018-05-04 15:30:52
2 2018-05-04 15:30:52

5. Convert string time to structured time

1  # ------->> Convert string time to structured time 
2  print (time.strptime( " 2018-5-4 16:10:9 " , " %Y-%m-%d % X " ))
time.struct_time(tm_year=2018, tm_mon=5, tm_mday=4, tm_hour=16, tm_min=10, tm_sec=9, tm_wday=4, tm_yday=124, tm_isdst=-1)

6. Default simplified string time (westernized form)

1 print(time.asctime())
2 print(time.ctime())
1 Fri May 4 15:32:39 2018
 2 Fri May 4 15:32:39 2018

7. It is also similar to stringified time

1 import datetime
2 print(datetime.datetime.now())
1 2018-05-04 15:33:57.990908

 

Guess you like

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