282

I've successfully converted something of 26 Sep 2012 format to 26-09-2012 using:

datetime.strptime(request.POST['sample_date'],'%d %b %Y')

However, I don't know how to set the hour and minute of something like the above to 11:59. Does anyone know how to do this?

Note, this can be a future date or any random one, not just the current date.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
user1678031
  • 2,925
  • 2
  • 16
  • 15

4 Answers4

553

Use datetime.replace:

from datetime import datetime
dt = datetime.strptime('26 Sep 2012', '%d %b %Y')
newdatetime = dt.replace(hour=11, minute=59)

Also worth noting: datetime.replace returns a new copy of the datetime (since datetime is immutable): it is like str.replace in that regard.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 4
    Yes, just do `date = datetime.strptime('26 Sep 2012', '%d %b %Y').replace(hour=11, minute=59)`. I used the extra variable so that the line wasn't too long :) – nneonneo Sep 18 '12 at 00:42
  • 28
    Also worth noting: `datetime.replace` returns a new *copy* of the datetime (since `datetime` is immutable): it is like `str.replace` in that regard. – nneonneo Sep 18 '12 at 00:43
  • How can i remove everything after '2016-06-01' so that hour minute and second disappear? – PV8 Mar 01 '19 at 14:05
  • 3
    @PV8 If you want a datetime, run: `r = dt.replace(hour=0, minute=0, second=0, microsecond=0)`. If you want a date object, run: `r = dt.date()` – Flimm Dec 09 '20 at 09:24
66

datetime.replace() will provide the best options. Also, it provides facility for replacing day, year, and month.

Suppose we have a datetime object and date is represented as: "2017-05-04"

>>> from datetime import datetime
>>> date = datetime.strptime('2017-05-04',"%Y-%m-%d")
>>> print(date)
2017-05-04 00:00:00
>>> date = date.replace(minute=59, hour=23, second=59, year=2018, month=6, day=1)
>>> print(date)
2018-06-01 23:59:59
Farhat Nawaz
  • 202
  • 5
  • 20
Abhay Chauhan
  • 693
  • 5
  • 3
  • 22
    What's your answer's added value against nneonneo's answer posted 5 years before yours ? – Samuel Dauzon Aug 23 '19 at 23:51
  • @SamuelDauzon, I guess it added magic internet points value to his user account. And, tbh, it added the date elements but that could be an edit to nneonneo's answer. – osiris Aug 06 '23 at 20:14
16

If you have a date as a datetime.datetime (or a datetime.date) instance and want to combine it via a time from a datetime.time instance, then you can use the classmethod datetime.datetime.combine:

import datetime
dt = datetime.datetime(2020, 7, 1)
t = datetime.time(12, 34)
combined = datetime.datetime.combine(dt.date(), t)
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
6

Shortcuts to convert datetime to max [23:59:59.99] or min [0:00:00]

import datetime

def dt_time_min(dt):
    """converts any datetime/date to new datetime with same date and time=0:00:00"""
    return datetime.datetime.combine(dt, datetime.time.min)


def dt_time_max(dt):
    """converts any datetime/date to new datetime with same date and time=23:59:59.999999"""
    return datetime.datetime.combine(dt, datetime.time.max)
pymen
  • 5,737
  • 44
  • 35