Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

492
Visualizações
Convert String with month name to datetime

I'm using pickadate.js which returns the date in this format:

8 March, 2017

I would like to convert this to the datetime format of:

yyyy-mm-dd

What would be the best way to do this in Python?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In Python, this is an easy exercise in using the datetime format strings:

from datetime import datetime

s = "8 March, 2017"
d = datetime.strptime(s, '%d %B, %Y')
print(d.strftime('%Y-%m-%d'))

See this table for a full description of all the format qualifiers.

Here I use the datetime.strptime method to convert your datepicker.js string to a Python datetime object. And I use the .strftime method to print that object out as a string using the format you desire. At first those format strings are going to be hard to remember, but you can always look them up. And they well organized, in the end.

I do wonder, though: might it be better to stay in JavaScript than switch over to Python for this last step? Of course, if you are using Python elsewhere in your process, this is an easy solution.

over 4 years ago · Santiago Trujillo Relatório

0

Mark Reed's advice might be best in your case. One frequently overlooked alternative for dates is the arrow module, which offers some interesting features. In this case you can do:

>>> import arrow
>>> arrow.get('8 March, 2017', 'D MMMM, YYYY').format('YYYY-MM-DD')
'2017-03-08'

As an example of a feature, if you capture that date you could format it in Russian.

>>> aDate = arrow.get('8 March, 2017', 'D MMMM, YYYY')
>>> aDate.format('YYYY MMMM DD', locale='ru')
'2017 марта 08'
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda