63

I have a date string and want to convert it to the date type:

I have tried to use datetime.datetime.strptime with the format that I want but it is returning the time with the conversion.

when = alldates[int(daypos[0])]
print when, type(when)

then = datetime.datetime.strptime(when, '%Y-%m-%d')
print then, type(then)

This is what the output returns:

2013-05-07 <type 'str'>
2013-05-07 00:00:00 <type 'datetime.datetime'>

I need to remove the time: 00:00:00.

xxx
  • 1,065
  • 11
  • 22
pythonhunter
  • 957
  • 3
  • 9
  • 23

8 Answers8

105
print then.date()

What you want is a datetime.date object. What you have is a datetime.datetime object. You can either change the object when you print as per above, or do the following when creating the object:

then = datetime.datetime.strptime(when, '%Y-%m-%d').date()
Woody Pride
  • 12,263
  • 8
  • 46
  • 60
  • Look here for keeping the datetime format: [link](https://stackoverflow.com/questions/5476065/how-to-truncate-the-time-on-a-datetime-object-in-python) – Carsten Jul 30 '19 at 07:31
10

If you need the result to be timezone-aware, you can use the replace() method of datetime objects. This preserves timezone, so you can do

>>> from django.utils import timezone
>>> now = timezone.now()
>>> now
datetime.datetime(2018, 8, 30, 14, 15, 43, 726252, tzinfo=<UTC>)
>>> now.replace(hour=0, minute=0, second=0, microsecond=0)
datetime.datetime(2018, 8, 30, 0, 0, tzinfo=<UTC>)

Note that this returns a new datetime object -- now remains unchanged.

ukrutt
  • 2,060
  • 3
  • 18
  • 27
9
>>> print then.date(), type(then.date())
2013-05-07 <type 'datetime.date'>
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
2

To convert a string into a date, the easiest way AFAIK is the dateutil module:

import dateutil.parser
datetime_object = dateutil.parser.parse("2013-05-07")

It can also handle time zones:

print(dateutil.parser.parse("2013-05-07"))
>>> datetime.datetime(2013, 5, 7, 1, 12, 12, tzinfo=tzutc())

If you have a datetime object, say:

import pytz
import datetime
now = datetime.datetime.now(pytz.UTC)

and you want chop off the time part, then I think it is easier to construct a new object instead of "substracting the time part". It is shorter and more bullet proof:

date_part datetime.datetime(now.year, now.month, now.day, tzinfo=now.tzinfo)

It also keeps the time zone information, it is easier to read and understand than a timedelta substraction, and you also have the option to give a different time zone in the same step (which makes sense, since you will have zero time part anyway).

nagylzs
  • 3,504
  • 6
  • 33
  • 60
1

For me, I needed to KEEP a timetime object because I was using UTC and it's a bit of a pain. So, this is what I ended up doing:

date = datetime.datetime.utcnow()

start_of_day = date - datetime.timedelta(
    hours=date.hour, 
    minutes=date.minute, 
    seconds=date.second, 
    microseconds=date.microsecond
)

end_of_day = start_of_day + datetime.timedelta(
    hours=23, 
    minutes=59, 
    seconds=59
)

Example output:

>>> date
datetime.datetime(2016, 10, 14, 17, 21, 5, 511600)
>>> start_of_day
datetime.datetime(2016, 10, 14, 0, 0)
>>> end_of_day
datetime.datetime(2016, 10, 14, 23, 59, 59)
Dovy
  • 1,230
  • 10
  • 18
0

You can use simply pd.to_datetime(then) and pandas will convert the date elements into ISO date format- [YYYY-MM-DD].

You can pass this as map/apply to use it in a dataframe/series too.

David Buck
  • 3,594
  • 33
  • 29
  • 34
zerryberry
  • 11
  • 3
0

You can usee the following code: week_start = str(datetime.today() - timedelta(days=datetime.today().weekday() % 7)).split(' ')[0]

0

If you specifically want a datetime and not a date but want the time zero'd out you could combine date with datetime.min.time()

Example:

datetime.datetime.combine(datetime.datetime.today().date(),
                          datetime.datetime.min.time())
Chris
  • 1,972
  • 1
  • 20
  • 35