Possible Duplicate:
Convert a datetime.date object into a datetime.datetime object with zeros for any missing time attributes
How do I convert a datetime.date
obj into datetime.datetime
obj, defaulting to midnight?
Using the datetime() Pass the year, month and day values of the desired date object (as my_date. year, my_date. month, my_date. day) to this constructor to convert a date object to datetime object.
Use strftime() function of a datetime class Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime.
Convert this datetime object to string in format 'DD-MMM-YYYY (HH:MM:SS:MICROS)' i.e. Format string used here is “%d-%b-%Y (%H:%M:%S. %f)“. The format string contains the codes pointing to each element of datetime like %d for day of month & %Y for year etc.
Here, we have used datetime.now() to get the current date and time. Then, we used strftime() to create a string representing date and time in another format.
Use the datetime.combine
method with an empty time
instance:
dateobject = datetime.date.today()
datetime.datetime.combine(dateobject, datetime.time())
Alternatively, you can use the datetime.time.min
constant:
datetime.datetime.combine(dateobject, datetime.time.min)
Both datetime.time()
and datetime.time.min
represent midnight (00:00:00).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With