Python related operation time

Related modules: Time, datetime, c alendar (calendar module)

Get the current time:

# Get the current time stamp 10, default return to float 
Print int (the time.time ())                     # Output: 1561790808 
# Get the current time stamp 13, default return to float 
Print int (the time.time () * 1000)              # the Output: 1,561,790,808,517 
# get the current time tuples, attributes are the current year, month, day, hour, minute, second, day of the week, when the first few days, whether or not daylight saving time 
# can directly call the property (eg. :. time.localtime () tm_year)
# pharmaceutically stamp parameter conversion
Print time.localtime () # Output: a time.struct_time (tm_year = 2019, tm_mon =. 6, tm_mday = 29, 14 = tm_hour, tm_min = 58, =. 17 tm_sec, tm_wday. 5 =, = 180 [tm_yday, the tm_isdst = 0)
# datetima module
print datetime.datetime.now () # output: 2019-06-29 15: 34: 03.184000

Formatting time request (formatted date symbol table link ):

# time模块
print
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # output:2019-06-29 14:59:57 print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # output:Sat Jun 29 14:59:57 2019
# datetime模块
print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # output:2019-06-29 14:59:57
# ISO格式时间
print datetime.datetime.now().isoformat() # output: 2019-06-29T15:39:55.599000

Timestamp string format conversion:

# String turn timestamp 
A = " Sat 28-Mar 2016 22:24:24 " 
Print time.mktime (the time.strptime (A, " % A% B% D% H:% M:% S% the Y " )) # output: 1459175064.0 (float type) 
# timestamp to string
A = 1459175064.0
Print The time.strftime ( "% D%% Y-M-% H:% M:% S", time.localtime (A)) # Output : 2016-03-28 22:24:24

 Time addition and subtraction:

# timedalta构造函数:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
print datetime.datetime.now().day                           # output:29
print datetime.datetime.now()-datetime.timedelta(days=1)    # output:2019-06-28 15:48:23.451000

 

Guess you like

Origin www.cnblogs.com/weswes/p/11106821.html