Convert date to string in date format

0

I'm trying to convert the date format

  

November 6, 2018

in

  

11/6/2018

as follows:

datetime.datetime.strptime(fecha_inicio_,'%d-%B-%Y')

and I get the following error, I was trying other convinations but I always get the same error.

  

ValueError: time data 'November 6, 2018' does not match format   '% d-% B-% Y'

    
asked by Sebastian 01.11.2018 в 17:17
source

2 answers

2

A strptime() you have to pass the string that specifies the format in which your text is supposed to be. You have set "%d-%B-%Y" , but that is not the text format, but it would be "%d de %B de %Y" .

However, since the name of the month appears in Spanish in your text, you will have to make sure you have set the locale of that language. Otherwise, possibly strptime() will wait november as the month name and it will also fail.

The following works for me:

import datetime
import locale

locale.setlocale(locale.LC_TIME, "es_ES")

fecha = "6 de noviembre de 2018"

t = datetime.datetime.strptime(fecha, "%d de %B de %Y")
print(t)
2018-11-06 00:00:00
    
answered by 01.11.2018 / 18:01
source
0

Inteta with this:

from datetime import datetime

s = "6 de Noviembre de 2018".replace("de","")
d = datetime.strptime(s, '%d %B %Y')
print(d.strftime('%d/%m/%Y'))
    
answered by 01.11.2018 в 17:51