Detailed explanation of Python datetime module, two practical classes of common time

Hi everyone, hello! I am a cheese who loves to touch fish ❤

datetime is a commonly used time module in python,
which is used to obtain time and
convert between time types.
Two practical classes are introduced below.

insert image description here

1. datetime.datetime class:

  • datetime.datetime.now():Return the current time of the system (2018-09-18 18:39:55.921602)

  • datetime.datetime.now().date():Returns the date at the current time (2018-09-18)

  • datetime.datetime.now().time():Returns the hour, minute and second of the current time (18:40:24.946237)

  • datetime.datetime.ctime():Convert datetime.datetime type to str type

testtime  = datetime.datetime.ctime(datetime.datetime.now())
print(type(testtime))
print(testtime)

#<class 'str'>
#Tue Sep 18 18:50:23 2018 python学习扣扣qun:540305994

Convert the time format to a string:

datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'):
return time string (2018-11-09 14:42:36)

Convert string to time format

datetime.datetime.strptime('2018-11-09 14:42:36','%Y-%m-%d %H:%M:%S'):
Return datetime.datetime type time (2018-11-09 14:42:36)

Symbols representing date formatting

  • %y two-digit year representation (00-99)

  • %Y four-digit year representation (000-9999)

  • %m month (01-12)

  • %d day of month (0-31)

  • %H Hours in 24-hour format (0-23)

  • %I Hours in 12-hour format (01-12)

  • %M minutes (00=59)

  • %S seconds (00-59)

  • %a local abbreviated weekday name

  • %A local full weekday name

  • %b local abbreviated month name

  • %B local full month name

  • %c local corresponding date representation and time representation

  • %j day of the year (001-366)

  • %p equivalent of local AM or PM

  • %U week number of the year (00-53) Sunday is the beginning of the week

  • %w week (0-6), Sunday is the beginning of the week

  • %W week number of the year (00-53) Monday is the beginning of the week

  • %x local corresponding date representation

  • %X local corresponding time representation

  • %Z the name of the current time zone

insert image description here

二、datetime.timedelta

datetime.timedelta

Used to calculate two datetime.datetime

Or the time difference between datetime.date types.

def __new__(cls, days=0, seconds=0, microseconds=0,    milliseconds=0, minutes=0, hours=0, weeks=0):

From the constructor point of view,

Optional parameters :days、seconds、microseconds、milliseconds、minutes、hours、weeks,且默认是0。

This function can easily solve the calculation problem between time.
For example, if we want to know the date of today 300 days ago,
it can be implemented like this:

>>> now = datetime.datetime.now().date()
>>> now
datetime.date(2018, 11, 9)
>>> delta = datetime.timedelta(days = 300)
>>> now - delta
datetime.date(2018, 1, 13)
datetime.timedelta()也可以多个参数,比如计算30012小时前的时间。


>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2018, 11, 9, 15, 30, 36, 156323)
>>> delta = datetime.timedelta(days = 300,hours = 12)
>>> now - delta
datetime.datetime(2018, 1, 13, 3, 30, 36, 156323)

计算总天数和秒数。

>>> datetime.timedelta(days=1,hours = 2).days
1
>>> datetime.timedelta(days=1,hours = 2).total_seconds()
93600.0

That's it for today's article~

I am Cheese who loves to touch fish❤, see you in the next article (✿◡‿◡)

insert image description here

Guess you like

Origin blog.csdn.net/m0_74872863/article/details/130013741