PyPI - Datetime
Posted kaimobile
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyPI - Datetime相关的知识,希望对你有一定的参考价值。
PyPI for Python 3.7
import datetime
timedelta Object
https://docs.python.org/3.7/library/datetime.html#timedelta-objects
Constructor:
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
It also has a lot of operations. See the Documents for detail.
date Object
https://docs.python.org/3.7/library/datetime.html#date-objects
Datetime object is a combination of a date and a time. I like to use datetime object instead.
datetime Object
https://docs.python.org/3.7/library/datetime.html#datetime-objects
Constructor:
datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
datetime.datetime.fromtimestamp(timestamp, tz=None)
,By timestampdatetime.datetime.combine(date, time, tzinfo)
,Combine date object and time objectdatetime.datetime.strptime(date_string, format)
,By string and format
Ordinal Date is the proleptic Gregorian ordinal of the date. It is the count from 0001-01-01, just like timestamp.
Instance methods:
(Notice: a is the name of a exist datetime object)
a.replace()
, Return a datetime with the same attributes, except for those attributes given new values.a.astimezone()
, Return a datetime object with the timezone you give.a.timetuple()
, Return a tuple. You can pick up what you need by index or others.a.toordinal()
, Return the ordinal.a.timestamp()
, Return the timestamp.a.strftime(format)
, Return the format string.
timezone Object
https://docs.python.org/3.7/library/datetime.html#timezone-objects
datetime.timezone(offset)
, The offset attribute must be timedelta.
String format:
(strftime --> string from time. strptime())
https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior
Directive | Tips | Meaning | Example |
---|---|---|---|
%Y |
Year | 4 bits Year | 0001, 0002,..., 2019 |
%m |
Month | 1/2 bits Month | 01, 02, ..., 12 |
%d |
Day | 1/2 bits Day | 01, 02, ..., 31 |
%H |
Hour | 2 bits Hour | 00, 01, ..., 23 |
%M |
Minute | 2 bits Minute | 00, 01, ..., 59 |
%S |
Second | 2 bits Second | 00, 01, ..., 59 |
%w |
Week | 1 bit, 0 for Sun. and 6 is Sat. | 0, 1, ..., 6 |
以上是关于PyPI - Datetime的主要内容,如果未能解决你的问题,请参考以下文章