-1

I have 2017-07-21 as a'datetime.date' object and

15:30:00 as a datetime.time' object

How do I combine the two to get

2017-07-21-15:30:00?

Mat.S
  • 1,814
  • 7
  • 24
  • 36
  • 2
    Possible duplicate of [Pythonic way to add datetime.date and datetime.time objects](https://stackoverflow.com/questions/8474670/pythonic-way-to-add-datetime-date-and-datetime-time-objects) – fredtantini Jul 23 '17 at 06:22

3 Answers3

2

classmethod datetime.combine(date, time, tzinfo=self.tzinfo)

Return a new datetime object whose date components are equal to the given date object’s, and whose time components are equal to the given time object’s.

source

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Use datetime.combine from datetime.

import datetime

d = datetime.date(2017, 07, 21)
t = datetime.time(15, 30, 0)
dt = datetime.datetime.combine(d, t)
Ikbel
  • 7,721
  • 3
  • 34
  • 45
0
     >>> from datetime import datetime, date, time

        >>> d = date(2017, 7, 21)
        >>> t = time(15, 30, 0)
        >>> newDate = datetime.combine(d, t)
        >>> newDate
        datetime.datetime(2017, 7, 21, 15, 30)
        >>> newDate.strftime(%Y-%m-%d-%H:%M:%S)
        '2017-07-21-15:30:00'

You can use the datetime.combine method as above. More about info here

anon
  • 1,258
  • 10
  • 17