Open In App

Python DateTime – time.replace() Method with Example

Last Updated : 23 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
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.006789

Example 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.006789

Example 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.006789

Example 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.006789

Example 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


Similar Reads

Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
Datetime.replace() Function in Python
Datetime.replace() function is used to replace the contents of the DateTime object with the given parameters. Syntax: Datetime_object.replace(year,month,day,hour,minute,second,microsecond,tzinfo) Parameters: year: New year value in range-[1,9999],month: New month value in range-[1,12],day: New day value in range-[1,31],hour: New hour value in range
2 min read
replace() Function Of Datetime.date Class In Python
replace() function is used to manipulate the object of DateTime class of module of DateTime. Generally, it replaces the date( Year, Month, Day) and returns a new DateTime object. Syntax: replace(year=self.year, month=self.month, day=self.day) Parameters: Year: New year value (range: 1 &lt;= year &lt;= 9999)month: New month value(range: 1 &lt;= mont
2 min read
Python datetime.tzname() Method with Example
tzname() method is used in the DateTime class of module DateTime. This method is used to return the time zone name of the DateTime object passed, as a string. Syntax: tzname() Return type: It will return the time zone name of the DateTime object passed, as a string. So first we need to import the module DateTime, and to get the timezone of the exac
2 min read
Python - datetime.toordinal() Method with Example
datetime.toordinal() is a simple method used to manipulate the objects of DateTime class. It returns proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. The function returns the ordinal value for the given DateTime object. If January 1 of year 1 has ordinal number 1 then, January 2 year 1 will have ordinal number 2, an
2 min read
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() Parameters: This function does not accept any para
3 min read
Python DateTime weekday() Method with Example
In this article, we will discuss the weekday() function in the DateTime module. weekday() function is used to get the week number based on given DateTime. It will return the number in the range of 0-6 RepresentationMeaning0Monday1Tuesday2Wednesday3Thursday4Friday5Saturday6Sunday It will take input as DateTime in the format of "(YYYY, MM, DD, HH, MM
3 min read
Python datetime.utcoffset() Method with Example
The utcoffset() function is used to return a timedelta object that represents the difference between the local time and UTC time. This function is used in used in the datetime class of module datetime.Here range of the utcoffset is "-timedelta(hours=24) &lt;= offset &lt;= timedelta(hours=24)".If the offset is east of UTC, then it is considered posi
2 min read
Article Tags :
Practice Tags :