Ali
Ali

Reputation: 3

How can I generate a date that includes the time?

I am trying to generate a date with range but the time is not showing. The code is as follows:

def date_range(start, end, step: datetime.timedelta):
   while start < end:
   yield start
   start += step


  for d in date_range(
    start=datetime.date(2019, 1, 1),
    end=datetime.date(2019, 1, 15),
    step=datetime.timedelta(days=7),
    ):
    print(d)



  >>>2019-01-01
  >>>2019-01-08

The code shows the date only without the time. Why???

Upvotes: 0

Views: 38

Answers (1)

cactus4
cactus4

Reputation: 128

You are using datetime.date() which, according to the Python documentation here does not include time information - it just sets the hours/minutes/seconds to 0.

I think you want to use datetime.datetime() which does include time information.

See the Python documentation on datetime.datetime() for more information.

Upvotes: 1

Related Questions