-1

I am sorry to ask this silly question but I am stuck in converting date format (Jan 02, 2020) to another date format ('%Y-%m-%d'). Would anyone be able to give me a hand here? Thank you very much.

Jacky Law
  • 53
  • 1
  • 1
  • 6

2 Answers2

1

Generally, this is a two-step process:

  • Convert the input into an internal representation, which in this case will almost certainly be either date or datetime. For simple cases where you know the format, the conversion can be done with the datetime.strptime() method.
  • Convert the internal representation into the required output form. In this case, that will be either date.strftime() or datetime.strftime(), depending on which internal representation you use; they both pretty much work the same way.
joshmeranda
  • 3,001
  • 2
  • 10
  • 24
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • I should note that this structure is applicable to a lot of problems in programming; many programs/functions have basically the same three-step structure: (1) deal with the input; (2) apply the needed transformation or calculation (in this case none); (3) prepare the output. – Jiří Baum Aug 24 '20 at 02:15
  • Thnak you sabik and puffin. This is helpful. I am working on them. – Jacky Law Aug 24 '20 at 03:13
1
import datetime
temp = datetime.datetime.strptime('Jan 02, 2020', '%b %d, %Y')
result = temp.strftime('%Y-%m-%d')
Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
Frank Yellin
  • 9,127
  • 1
  • 12
  • 22