How to format dates in python?

2

When I do this:

  data = input('data [d/m/Y]: ')    
  print(data)

  data2 = datetime.strptime(data, "%d/%m/%Y")

  print(data2)

It returns me like this:

data [d/m/Y]: 17/08/2018

17/08/2018

2018-08-17 00:00:00

How do I format the date being dd / mm / yyyy and the time together does not appear?

    
asked by anonymous 18.09.2018 / 20:08

2 answers

6

The method datetime.strptime serves to parse parsing (probably because of the P) of a date in a given format. That is, it gets a string, parses, and returns an datime object. .

To transform a datetime object into a string again you can use:

See here the code running.

    
18.09.2018 / 20:48
1

If you want to use print try:

import datetime

_data="12/09/2018"
_data2=datetime.datetime.strptime(_data, "%d/%m/%Y")
print(_data2)

2018-09-12 00:00:00

print("{}/{}/{}".format(_data2.day,_data2.month,_data2.year))

12/9/2018
    
18.09.2018 / 20:45