Use dates in Python

I learned a little about Python's datetime module. This time only the date object.

From the official Python documentation

The datetime module provides classes for manipulating date and time data in both simple and complex ways. While four arithmetic operations on dates and times are supported, this module implementation focuses on efficient retrieval of attributes for output formatting and manipulation.

date object

datetime.date(year, month, day)

All arguments are required. A date object with the date and time specified by the argument is created.

date.today()

Returns the current local date.

date.fromtimestamp(timestamp)

Returns the local date corresponding to the POSIX timestamp given in the argument. If the timestamp is outside the range of values supported by the platform C function localtime (), it may throw an OverflowError. If the call to localtime () fails, OSError may be thrown.

date.replace(year, month, day)

Returns a data object with the parameters specified by the keyword arguments replaced.

date.weekday()

Returns the day of the week as an integer, with Monday as 0 and Sunday as 6.

date.isoweekday()

Returns an integer with Monday as 1 and Sunday as 7.

date.strftime(format)

Returns a character string that represents the date according to the format character string specified by format.

>>> import datetime

datetime.date (2000, 1, 1) # Create a date object that represents January 1, 2000 datetime.date(2000, 1, 1)

datetime.date.today () # Create a date object that represents today's date datetime.date(2016, 5, 6)

d = datetime.date.today() d datetime.date(2016, 5, 6)

d.replace (year = 1990) # date Change the year of the object datetime.date(1990, 5, 6)

d.weekday () # Returns the day of the week as an integer (with Monday as 0) 4

d.isoweekday () # Returns the day of the week as an integer (with Monday as 1) 5

d.strftime ('% Y-% m-% d') # Display in the format given to the argument '2016-05-06'

Recommended Posts

Use dates in Python
Use config.ini in Python
Use Valgrind in Python
Use profiler in Python
Let's use def in python
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Use fabric as is in python (fabric3)
How to use SQLite in Python
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use OpenCV with Python 3 in Window
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python