Get a datetime instance at any time of the day in Python

Premise

Import datetime in advance.

from datetime import datetime

Datetime instance at 0:00:00 of the day

Use replace to force it to 0:00:00 of the day.

datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)

Execution result

>>> datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)
datetime.datetime(2015, 8, 22, 0, 0)

application

Think about the time zone

I will try to correspond to the time zone using pytz

>>> import pytz
>>> tz = pytz.timezone('Asia/Tokyo')
>>> datetime.now(tz).replace(hour=0,minute=0,second=0,microsecond=0)
datetime.datetime(2015, 8, 22, 0, 0, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)

Datetime instance at 0:00:00 on New Year's Day of the year

In addition to the above, January 1st is fine, so it is as follows.

>>> import pytz
>>> tz = pytz.timezone('Asia/Tokyo')
>>> datetime.now(tz).replace(month=1,day=1,hour=0,minute=0,second=0,microsecond=0)
datetime.datetime(2015, 1, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)

Recommended Posts

Get a datetime instance at any time of the day in Python
Get the caller of a function in Python
Get the number of specific elements in a python list
[python] Get a list of instance variables
A function that measures the processing time of a method in python
Get the number of readers of a treatise on Mendeley in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Get a capture of the entire web page in Selenium Python VBA
[Python] Get the files in a folder with Python
Make a copy of the list in Python
Output in the form of a python array
Get a glimpse of machine learning in Python
At the time of python update on ubuntu
[Python] Get the day of the week (English & Japanese)
Count the number of times two values appear in a Python 3 iterator type element at the same time
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Turn multiple lists with a for statement at the same time in Python
How to get a list of files in the same directory with python
How to get the number of digits in Python
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Find out the apparent width of a string in python
Get UNIXTIME at the beginning of today with a command
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Get the current date and time in Python, considering the time difference
Find the eigenvalues of a real symmetric matrix in Python
Get the index of each element of the confusion matrix in Python
Python> Get a list of files in multiple directories> Use glob | Sort by modification time
Get the desktop path in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
Get the script path in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
Get the host name in Python
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
Create an instance of a predefined class from a string in Python
How to determine the existence of a selenium element in Python
I compared the calculation time of the moving average written in Python
Try to get a list of breaking news threads in Python.
How to check the memory size of a variable in Python
Read the standard output of a subprocess line by line in Python
[Python] Get the update date of a news article from HTML
[python] Get the rank of the values in List in ascending / descending order
[Python3] Define a decorator to measure the execution time of a function
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
Get the formula in an excel file as a string in Python
Take a look at the built-in exception tree structure in Python 3.8.2
Get a list of files in a folder with python without a path
Get the title and delivery date of Yahoo! News in Python
Put the process to sleep for a certain period of time (seconds) or more in Python
python memo: enumerate () -get index and element of list at the same time and turn for statement
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
Display a list of alphabets in Python 3
Run the Python interpreter in a script
The result of installing python in Anaconda
How to get a stacktrace in python