Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I do convert from timestamp to date in python?

I have this string '2015-04-08T07:52:00Z' and I wanna to convert it to '08/04/2015', how can I do this?

like image 332
Natali Torres Avatar asked Apr 17 '15 01:04

Natali Torres


People also ask

How do I convert timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

How do I extract a date from a timestamp in Python?

For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt. date from Pandas in Python.

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

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.


1 Answers

You can use the datetime.datetime.strptime() function to create a datetime object, then datetime.datetime.strftime() to return your correctly formatted date like so:

from datetime import datetime
dt = datetime.strptime('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ')
print dt.strftime('%d/%m/%Y')
like image 78
Vorticity Avatar answered Sep 29 '22 06:09

Vorticity