Get date in Python

Purpose: Manipulate the date as you wish

I thought it would be necessary when manipulating the date of csv output by scraping in the future.

What you can get from this article

-You can specify the date in the csv name to be created by using the current time in the application. -Operation of csv written in the csv file -Date type to string type and vice versa

Output the current time

test.py


from datetime import datetime
now = datetime.now()
print(now)



-----result-----
2020-10-26 20:39:01.825495

How to change a string to a date

test.py


from datetime import datetime

#  2018/11/11 can be done in the same way
str_date = 'November 11, 2018'



#strptime turns a string into a date
#strftime turns the date into a string

one_date = datetime.strptime(str_date, '%Y year%m month%d day')
print(one_date)


-----result-----
2018-11-11 00:00:00

Past and future date manipulation

``test.py`


from datetime import datetime
from datetime import timedelta

str_date = '2018/11/11'
base_date = datetime.strptime(str_date, '%Y/%m/%d')

print(base_date)

-----result-----
2018-11-11 00:00:00
----------------


#10 days ago
before_10days = base_date - timedelta(days=10)
#10 days later
before_10days = base_date - timedelta(days=10)

print(before_10days)


-----result-----
2018-11-01 00:00:00

Manipulating dates using csv

day.csv


2016-10-01
2016-10-02
2016-10-03
2016-10-04
2016-10-05
2016-10-06
2016-10-07
2016-10-08
2016-10-09
2016-10-10
2016-10-11
2016-10-12
2016-10-13
2016-10-14

day.py


from datetime import datetime

#read csv
with open('day.csv', encoding='utf-8') as f:

    for row in f:
        day = datetime.strptime(row.rstrip(), '%Y-%m-%d')
        print(f'{day:%Y/%m/%d}')





-----result-----
2016/10/02
2016/10/03
2016/10/04
2016/10/05
2016/10/06
2016/10/07
2016/10/08
2016/10/09
2016/10/10
2016/10/11
2016/10/12
2016/10/13
2016/10/14

Python features

What's new in Python 3.8 f'{Expression =}' The expression itself can now be output. When outputting the format f'{expression =: format}'

Output the corresponding date from csv

test.py


from datetime import datetime
from datetime import timedelta

main_date ='2016-10-12'

#String → date
base_day = datetime.strptime(main_date,'%Y-%m-%d')

#Date one week ago
before_7days = base_day - timedelta(days=7)

#Select csv file
with open('day.csv', encoding='utf-8') as f:
 
    for row in f:

        #Character string → date Delete line break at the end of line
        day = datetime.strptime(row.rstrip(), '%Y-%m-%d')
        
        #print(day)
        #2016-10-02 00:00:00
        #2016-10-03 00:00:00
                #~
        #2016-10-14 00:00:00


        #2016-10-Show applicable items for 12 to 7 days
        if before_7days <= day < base_day:
                #Y/m/In d format
            print(f'{day:%Y/%m/%d}')


-----reult-----
2016/10/05
2016/10/06
2016/10/07
2016/10/08
2016/10/09
2016/10/10
2016/10/11


Recommended Posts

Get date in Python
Date manipulation in Python
[Python3] Get date diff
Date calculation in python
Get date with python
Date calculation in Python
Get YouTube Comments in Python
Get last month in python
String date manipulation in Python
Get Evernote notes in Python
Sort by date in python
Get Japanese synonyms in Python
Get Leap Motion data in Python.
Get data from Quandl in Python
Get the desktop path in Python
Get the script path in Python
Get, post communication memo in Python
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python date arithmetic
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Get the current date and time in Python, considering the time difference
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
DirectLiNGAM in Python