0

how to convert python date format to 10 digit date format for mysql

example: date in python -> 11-05-09

to something like 1239992972 (10 digit)

Thanks

4 Answers 4

4

You can use the time module's strptime - you pass in a string time (e.g. 11-05-09) and a format and it will return a struct_time, which you can get the numerical value from (by calling time.mktime on the returned struct_time). See the docs for time.strptime for further details.

Sign up to request clarification or add additional context in comments.

Comments

2

If it is a datetime obj, you can do:

import time
time.mktime(datetime_obj.timetuple())

If not:

time.mktime(time.strptime("11-05-09", "%d-%m-%y"))

Comments

0

Use time.strptime() to convert the date into a time tuple, and then time.mktime() (or calendar.timegm()) to convert that into a floating point time. You'll probably need to truncate it to an integer after.

tm = time.strptime('11-05-09', '%d-%m-%y')
time = time.mktime(tm)
time_int = int(time)

http://docs.python.org/library/time.html

1 Comment

your format and date string are swapped, and there's no %f identifier
0

You can use easy_date to make it easy:

import date_converter
timestamp = date_converter.string_to_timestamp('11-05-09','%d-%m-%y')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.