Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python datetime round to nearest hour

from datetime import datetime, timedelta

now = datetime.now()

def hour_rounder(t):
    # Rounds to nearest hour by adding a timedelta hour if minute >= 30
    return (t.replace(second=0, microsecond=0, minute=0, hour=t.hour)
               +timedelta(hours=t.minute//30))

print(now)
print(hour_rounder(now))
Comment

PREVIOUS NEXT
Code Example
Python :: replit clear 
Python :: python array delete last column 
Python :: squared sum of all elements in list python 
Python :: python float till 2 decimal places 
Python :: python copy dir 
Python :: pip install apache beam gcp 
Python :: pd.set_option show all rows 
Python :: tkinter maximum window size 
Python :: list to csv pandas 
Python :: how to load ui file in pyqt5 
Python :: python string list to list 
Python :: remove commas from string python 
Python :: covariance matrix python 
Python :: sort_values 
Python :: python first day of last month 
Python :: confusion matrix seaborn 
Python :: panda dataframe to list 
Python :: how to do pandas profiling 
Python :: python alfabet 
Python :: how to plot a graph using matplotlib 
Python :: how to make a text input box python pygame 
Python :: ver todas linhas dataframe pandas 
Python :: how to pass header in requests 
Python :: how to sort in pandas 
Python :: select DF columns python 
Python :: python get minute from datetime 
Python :: upgrade python to 3.8 
Python :: pandas dataframe histogram 
Python :: how to get the current web page link in selenium pthon 
Python :: extract only year from date python 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =