Python DateTime - time.replace() Method with Example Last Updated : 23 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss the time.replace() method in Python. This method is used to manipulate objects of time class of module datetime. It is used to replace the time with the same value, except for those parameters given new values by whichever keyword arguments. Syntax: replace(year=self.year, month=self.month, day=self.day) Parameters: Year: New year value (range: 1 <= year <= 9999)month: New month value(range: 1 <= month <= 12)day: New day value(range: 1<= day <= 31) Returns: New datetime object. Python program to get time and display: Python3 # import time from datetime from datetime import time # create time x = time(5,34,7,6789) print("Time:", x) Output: Time: 05:34:07.006789Example 1: Python program to replace hour from the given time Python3 # import time from datetime from datetime import time # create time x = time(5, 34, 7, 6789) print("Actual Time:", x) # replace hour from 5 to 10 final = x.replace(hour = 10) print("New time after changing the hour:", final) Output: Actual Time: 05:34:07.006789 New time after changing the hour: 10:34:07.006789Example 2: Python program to replace minute from time Python3 # import time from datetime from datetime import time # create time x = time(5, 34, 7, 6789) print("Actual Time:", x) # replace minute from 34 to 12 final = x.replace(minute = 12) print("New time after changing the minute:", final) Output: Actual Time: 05:34:07.006789 New time after changing the minute: 05:12:07.006789Example 3: Python code to replace seconds Python3 # import time from datetime from datetime import time # create time x = time(5,34,7,6789) print("Actual Time:", x) # replace second from 7 to 2 final = x.replace(second = 2) print("New time after changing the second:", final) Output: Actual Time: 05:34:07.006789 New time after changing the second: 05:34:02.006789Example 4: Python program to replace all at a time Python3 # import time from datetime from datetime import time # create time x = time(5,34,7,6789) print("Actual Time:", x) # replace hour from 5 to 10 # replace minute from 34 to 11 # replace second from 7 to 1 # replace milli second from 6789 to 1234 final = x.replace(hour = 10, minute = 11, second = 1, microsecond = 1234) print("New time :", final) Output: Actual Time: 05:34:07.006789 New time : 10:11:01.001234 Comment More infoAdvertise with us Next Article Manipulate Date and Time with the Datetime Module in Python S sravankumar_171fa07058 Follow Improve Article Tags : Python Python-datetime Practice Tags : python Similar Reads Python datetime.timetz() Method with Example The timetz() function manipulates the objects of the DateTime class of the DateTime module. This function uses an instance method given to it via reference, and converts them, and returns a time object with the same hour, minute, second, microsecond, and fold and tzinfo attributes. Syntax: timetz() 3 min read Python timedelta total_seconds() Method with Example The total_seconds() function is used to return the total number of seconds covered for the specified duration of time instance. This function is used in the timedelta class of module DateTime. Syntax: total_seconds() Parameters: This function does not accept any parameter. Return values: This functi 2 min read Manipulate Date and Time with the Datetime Module in Python Have you ever wondered about working with Date and Time with Python? If you have then you must have noticed that Python does not provide any built-in method to work with either Date or Time. But thanks to the DateTime module that comes pre-loaded with Python's standard utility modules we can easily 9 min read Python | time.asctime() method Python time method time.asctime() is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form: Day Mon Date Hour:Min:Sec Year For example: Thu 08 22 10:46:56 2019Python time asctime() Syntax: ti 3 min read Python - time.ctime() Method Python time.ctime() method converts a time in seconds since the epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). Current time is returned by localtime() is used when the time tuple is not present. Syntax: time.ctime([ sec ]) Parameter: sec: number of seconds to be 2 min read Datetime.replace() Function in Python The datetime.replace() method in Python allows you to modify specific parts of a datetime or date object without changing the original object. Instead, it returns a new modified object with the updated values.For example, suppose we booked a flight for March 15, 2025, at 10:00 AM, but it got resched 3 min read Like