-3
Date = datetime.datetime.today().strftime("%d %B %Y")
x = datetime.datetime.strptime(Date , "%d %B %Y")

returns:

2018-05-09 00:00:00

instead of: 9 May 2018, what am I doing wrong? Essentially I am trying to get the non zero padded version of strftime for which I searched around and I read that the way to go about it is strptime.

codeforester
  • 39,467
  • 16
  • 112
  • 140
bloo
  • 306
  • 4
  • 13
  • 1
    print( `datetime.datetime.today().strftime("%d %B %Y"))` – Rakesh May 09 '18 at 15:20
  • but that returns 09 May 2018, as per my question above, I am trying to get the non-zero padded version of the date eg. 9 May 2018 – bloo May 09 '18 at 15:22
  • 1
    I don't think it's supported out of the box by python see documentation here for all the available features of strftime (https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). – Yensa May 09 '18 at 15:24
  • @bloo People are probably downvoting the question because you haven’t explained why you’re calling `strptime` to turn the value back into the very same `datetime` object you started with, or what you expected that to do. Also, it seems like you want to know how to fix the zero padding on `Date`, but that’s not what you asked about—instead you showed us the string representation of `x`, and asked how to get the format you already have in `Date` – abarnert May 09 '18 at 15:55
  • In other words, they’re not downvoting because they don’t know the answer, they’re downvoting because, as asked, it looks like you’re just asking “how do I not do this second line that I copied and pasted somewhere without understanding it”, and the answer to that is just “don’t do that line”. You _do_ actually have a good question here, but you need to edit it to make that question clear. You probably won’t get those downvotes back (most people who downvote never see the question again), but you may get upvotes that more than compensate for it. – abarnert May 09 '18 at 15:57

1 Answers1

3

A possible solution is to use str.lstrip

Ex:

import datetime
Date = datetime.datetime.today().strftime("%d %B %Y")
print(Date.lstrip("0"))

Output:

9 May 2018
Rakesh
  • 81,458
  • 17
  • 76
  • 113