I thought it would be useful to have a script that automatically enters the date of attendance management. I wrote the following using strptime of python's datetime module.
Kinkyu.py
input_date = input('Please enter the year and month to create(Example:201608):')
try:
month_first = datetime.datetime.strptime(input_date, '%Y%m')
except ValueError:
input('Enter the year and month as 201608.')
sys.exit()
Then, the following error occurs when entering the year and month.
month_first = datetime.datetime.strptime(input_date, '%y%m')
TypeError: must be string, not int
that? I thought and looked it up Apparently, the first argument of strptime is a character string. You can't use numbers ...
If the int type is NG, why not convert it to a String type? I thought I changed it as follows.
Kinkyu.py
input_date = input('Please enter the year and month to create(Example:201608):')
input_date = str(input_date) #Changes: Convert numbers to strings
try:
month_first = datetime.datetime.strptime(input_date, '%Y%m')
except ValueError:
input('Enter the year and month as 201608.')
sys.exit()
This is the solution. Type conversion is easy with python.
8.1. datetime — Basic date and time types (from python standard library) https://docs.python.jp/3/library/datetime.html
Create a datetime object from a string in Python (Python 3.3) http://qiita.com/BarutanGNE/items/b14bef93c004f90c0ce4