Common built-in modules - Time Module

1.datetime processing date and time

Get the current date and time

1 from datetime import datetime
2 now=datetime.now()
3 print(now)

 

Gets the specified date and time

1 from datetime import datetime
2 
3 dt=datetime(2019,6,26,17,14)
4 print(dt)

 

timestamp datetime turn turn local time timestamp, the timestamp value has nothing to do with the time zone, computer storage is the timestamp, 1970 Nian 1 Yue 1, 00:00:00 (Greenwich Mean Time)

1 from datetime import datetime
2 
3 dt=datetime.now()
4 print(dt.timestamp())

 

timestamp转datetime

. 1  from datetime Import datetime
 2  
. 3 T = 1234567890
 . 4  Print (datetime.fromtimestamp (T)) # local time, East region 8 
. 5  Print (datetime.utcfromtimestamp (T))   # Greene time, time zone to UTC

 

str turn datetime

1 from datetime import datetime
2 
3 d = datetime.strptime('2019.6.26 17:30:00','%Y.%m.%d %H:%M:%S')
4 print(d)

 

datetime turn str

. 1  from datetime Import datetime
 2  
. 3 now = DateTime.Now ()
 . 4  Print (now.strftime ( ' % A,% B,% D% H:% M:% S ' )) # week, month, day,

 

Date output formats

the date and time format python symbol:

 % Y represent two-digit year (00-99 ) %

 the Y represents a four-digit year (000-9999 ) %

 m (01-12 ) 

within% d month of day ( 0-31 of ) %

 H 24 hours (0-23 hours, Ltd. ) %

 h 12 is the number of the I (manufactured by 01-12 hours ) %

 M number of minutes (00 = 59 ) %

 S seconds (00-59 )

 

 % A simplified local weeks name

 % a full week of local name

 % B local simplified month name

 % B full month name local

 % C indicates the corresponding local date and time indicates

 one day (001-366% j years )

 % P AM or PM local equivalent Fu

 weeks of the year U% (00-53 ) on Sunday as the start of the week

 % w week (0-6 ), Sunday to begin the week of

 % W week number of the year (00-53 ) on Monday for the week start

 % X represents a date local

 %X represents a local time corresponding

 % name of the current time zone Z

 %% number itself%
View Code

 

datetime subtraction introduced directly timedelta + -

1 from datetime import datetime,timedelta
2 
3 now = datetime.now()
4 print(now+timedelta(days=1000,hours=1))

 

Local time turn UTC time

1 from datetime import datetime,timedelta,timezone
2 
3 tz_utc_8 = timezone(timedelta(hours=8))#创建时区UTC+8:00
4 now = datetime.now()
5 print(now.replace(tzinfo=tz_utc_8))

 

Guess you like

Origin www.cnblogs.com/bfcs/p/11091467.html