0

I have an str variable named day and I want to convert it to date

day = "09FEB2017"
name
  • 41
  • 1
  • 5

3 Answers3

2

Use the appropriates format codes

day = "09FEB2017"
d = datetime.strptime(day, "%d%b%Y")
print(d)         # 2017-02-09 00:00:00
print(d.date())  # 2017-02-09 
azro
  • 53,056
  • 7
  • 34
  • 70
1
from datetime import datetime

day = "09FEB2017"
datetime_object = datetime.strptime(day, '%d%b%Y')

does the trick.

Oleksii Tambovtsev
  • 2,666
  • 1
  • 3
  • 21
-1

Already answered here. https://stackoverflow.com/a/466376/7539615

datetime.strptime will come in handy

Arpit T
  • 1
  • 1
  • It looks like this answer points out that a different Q&A already solves this question. If this is the case, please flag or vote this question as a *duplicate* of the other Q&A. – MisterMiyagi Dec 27 '21 at 13:51
  • I tried to mark this as duplicate but was unable to do so. I pasted the link here which clearly explains how strings of different types can be converted to date, but somehow feels that it offended someone. Anyways, maybe half info is better than complete knowledge. – Arpit T Dec 27 '21 at 13:59