Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to Date Format as DD/MM/YYY in Python - Portuguese

I would like to convert the string "23/02/2018" to a date format as 23-fev-2018.

Most importanly is that, the month must be in portuguese language, refering to fevereiro.

My issue is that usually the datetime.date prints like (YYYY,MM,DD):

import datetime 
datestr = "23/02/2018" 
dateobj = datetime.datetime.strptime(datestr, "%d/%m/%Y")
print dateobj #year, month, day

How may I print from a string as 23/10/2017 to date format as 23-out-2017, refering to the month "outubro" in portuguese?

like image 834
Ricardo Marques Avatar asked Mar 12 '18 23:03

Ricardo Marques


People also ask

How do I print a date in dd mm yyyy format in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I convert a date string to a date in Python?

We use the strptime() method to convert a date string to a date object.

What is the difference between Strftime and Strptime in Python?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

How do I check if a date is in mm/dd/yyyy in Python?

Method #1 : Using strptime() In this, the function, strptime usually used for conversion of string date to datetime object, is used as when it doesn't match the format or date, raises the ValueError, and hence can be used to compute for validity.


1 Answers

Use the locale module.

import locale
import datetime

locale.setlocale(locale.LC_ALL, 'pt_pt.UTF-8')
datetime.datetime.strptime('23/10/2017', '%d/%m/%Y').strftime('%d/%B/%Y')
# '23/Outubro/2017'
datetime.datetime.strptime('23/10/2017', '%d/%m/%Y').strftime('%d/%b/%Y')
# '23/Out/2017'
like image 112
jackotonye Avatar answered Oct 20 '22 22:10

jackotonye