2
fmt_1 = '%a %d %B %Y %H:%M:%S %z'
fmt_2 = '%a %d %b %Y %H:%M:%S %z'

t1="Sat 14 Sep 2126 00:36:44 +1400"
code1= dateime.datime.strptime(t1,fmt_1)  #run time error

code2= dateime.datime.strptime(t1,fmt_2)   #right code

I got runtime error for fmt_1 for this and fixed by using fmt_2

what is difference between %b and %B?

Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
  • Make it into a habit of **posting the full stack trace** please. Makes life easier when you want help. – Torxed May 09 '20 at 15:24
  • I'm assuming you notice the typo in datetime.datetime? Can you fix it and try posting the error again? – Prateek Dewan May 09 '20 at 15:25
  • 1
    Simply [read the docs](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) as they are well defined there. But as Prateek said, your issue is most likely just a spelling issue. – Torxed May 09 '20 at 15:25
  • Additionally, %b signifies abbreviated month name (just the first 3 letters), while %B signifies the full month name. That's probably what you're looking for. – Prateek Dewan May 09 '20 at 15:28

3 Answers3

1

From the documentation about Format Codes

  • %b is for Month as locale’s abbreviated name. Like Jan, Feb, …, Dec

  • %B is for Month as locale’s full name. Like January, February, …, December


In Sat 14 Sep 2126 00:36:44 +1400 the month Sep is in abbreviated format so %b fits, wher %B doesn't

azro
  • 53,056
  • 7
  • 34
  • 70
1

%b use for locale’s abbreviated month name and %B use for full month name. t1="Sat 14 Sep 2126 00:36:44 +1400" is this string Sep is short name. that's why you are getting error in fmt_1 for format mismatch. You can follow this doc https://docs.python.org/2.6/library/datetime.html

-1

you can use this:

datetime.datetime.strptime("Sat 14 Sep 2126 00:36:44 +1400", '%a %d %b %Y %H:%M:%S %z').strftime("%Y-%m-%d %H:%M:%S")

and get the following result:

'2126-09-14 00:36:44'