Django - String to Date - Date to UNIX Timestamp

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

How can I do it? I can not find any resources that show the date in this format? Thanks.

+4
source share
2 answers

You need the strptime method. If you use Python 2.5 or higher, this is a datetime method, otherwise you must use a combination of time and datetime modules for 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]) 
+12
source

You can use the time.strptime() method to parse a date string. This will return time_struct , which you can pass to time.mktime() (when the line represents local time) or calendar.timegm() (when the line is UTC) to get the number of seconds since the era.

+2
source

All Articles