Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - String to Date - Date to UNIX Timestamp

Tags:

python

django

I need to convert a date from a string (entered into a url) in the form of 12/09/2008-12:40:49. Obviously, I'll need a UNIX Timestamp at the end of it, but before I get that I need the Date object first.

How do I do this? I can't find any resources that show the date in that format? Thank you.

like image 719
Federer Avatar asked Dec 09 '22 19:12

Federer


1 Answers

You need the strptime method. If you're on Python 2.5 or higher, this is a method on datetime, otherwise you have to use a combination of the time and datetime modules to achieve this.

Python 2.5 up:

from datetime import datetime
dt = datetime.strptime(s, "%d/%m/%Y-%H:%M:%S")

below 2.5:

from datetime import datetime
from time import strptime
dt = datetime(*strptime(s, "%d/%m/%Y-%H:%M:%S")[0:6])
like image 146
Daniel Roseman Avatar answered Dec 26 '22 16:12

Daniel Roseman