Embedding in datetime when only the time is known

Overview

In this article, I was able to get the time when scraping, so I would like to post a solution when I want to add the date and time to that time and convert it to datetime type.

Solution 1

Conversion from datetime to string

dt_now = dt.now()
time_str = dt_now.strftime('%Y/%m/%d')

This enables the following conversions. 2018-02-02 18:31:13→18/02/02 I think that you should combine the acquired value with this character string and then use strptime to convert it to datetime type.

** However, in my execution environment,% d inevitably causes an error, so I tried another method. ** **

Solution 2

Split datetime

dt_now = dt.now()

year = int(dt_now.year)
month = int(dt_now.month)
day = int(dt_now.day)

Combine these with the value obtained by scraping converted to int type.

datetime_after = dt(year,month,day,hour,minute)

Summary

The first step is to fix% d, but if it seems to fit, you may try Solution 2.

Recommended Posts

Embedding in datetime when only the time is known
When the target is Ubuntu 16.04 in Ansible
When the selected object in bpy.context.selected_objects is not returned
Creating a list when the nomenclature is a fixed time
What to do when only the window is displayed and nothing is displayed in pygame Note
Precautions when changing unix time to datetime type in pandas
[Python] Execution time when a function is entered in a dictionary value
[Golang] "package exec is not in GOROOT" when executing the test
The solution when an empty object is returned in go's json.Marshal
Animate what happens in frequency space when the Nyquist frequency is exceeded
When the node disappears in rqt_graph
What to do when the value type is ambiguous in Python?
What to do when the result downloaded via scrapy is in English
Notify using Notification Center when the execution environment is macOS in Python
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
Get a datetime instance at any time of the day in Python
A useful note when using Python for the first time in a while
Behavior when returning in the with block
What is "mahjong" in the Python library? ??
MongoDB for the first time in Python
When the previous graph remains in Seaborn
Is there NaN in the pandas DataFrame?
The date is displayed incorrectly in matplotlib.
[pandas] When specifying the default Index label in the at method, "" is not required
Are Php / Ruby / Python that only runs when the file is called directly
What I did when I got stuck in the time limit with lambda python
Differences in the behavior of each LL language when the list index is skipped
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Pipfile is not created in the current directory
What is wheezy in the Docker Python image?
Time when terminal disappears in Visual Studio Code
When searching is not working in GAE's Datastore
About the difference between "==" and "is" in python
[Django] Error when SlugField is specified in .filter ()
Read the output of subprocess.Popen in real time
Precautions when installing packages in the conda environment
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
When the axis and label overlap in matplotlib
Loop variables at the same time in the template
This is the only basic review of Python ~ 3 ~
Behavior when saving python datetime object in MongoDB
virtualenv For the time being, this is all!
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
[Solution] When inserting "0001" into the column of string in sqlite3, it is entered as "1".
Import audit.log into Splunk and check the behavior when Splunk is started for the first time
What to do when is not in the sudoers file.This incident will be reported.
When a local variable with the same name as a global variable is defined in the function