You can download this code by clicking the button below.
This code is now available for download.
This function converts a date string and a time string into a datetime object.
Technology Stack : datetime
Code Type : Function
Code Difficulty : Intermediate
import datetime
def time_converter(date_str, time_str):
try:
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d')
time_obj = datetime.datetime.strptime(time_str, '%H:%M:%S')
return datetime.datetime.combine(date_obj, time_obj)
except ValueError:
return "Invalid date or time format."