SaltyCrane Blog — Notes on JavaScript and web development

Python datetime / time conversions

from datetime import datetime
import time

#-------------------------------------------------
# conversions to strings
#-------------------------------------------------
# datetime object to string
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
print date_str

# time tuple to string
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
print date_str

#-------------------------------------------------
# conversions to datetime objects
#-------------------------------------------------
# time tuple to datetime object
time_tuple = (2008, 11, 12, 13, 51, 18, 2, 317, 0)
dt_obj = datetime(*time_tuple[0:6])
print repr(dt_obj)

# date string to datetime object
date_str = "2008-11-10 17:53:59"
dt_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(dt_obj)

# timestamp to datetime object in local time
timestamp = 1226527167.595983
dt_obj = datetime.fromtimestamp(timestamp)
print repr(dt_obj)

# timestamp to datetime object in UTC
timestamp = 1226527167.595983
dt_obj = datetime.utcfromtimestamp(timestamp)
print repr(dt_obj)

#-------------------------------------------------
# conversions to time tuples
#-------------------------------------------------
# datetime object to time tuple
dt_obj = datetime(2008, 11, 10, 17, 53, 59)
time_tuple = dt_obj.timetuple()
print repr(time_tuple)

# string to time tuple
date_str = "2008-11-10 17:53:59"
time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print repr(time_tuple)

# timestamp to time tuple in UTC
timestamp = 1226527167.595983
time_tuple = time.gmtime(timestamp)
print repr(time_tuple)

# timestamp to time tuple in local time
timestamp = 1226527167.595983
time_tuple = time.localtime(timestamp)
print repr(time_tuple)

#-------------------------------------------------
# conversions to timestamps
#-------------------------------------------------
# time tuple in local time to timestamp
time_tuple = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp = time.mktime(time_tuple)
print repr(timestamp)

# time tuple in utc time to timestamp
time_tuple_utc = (2008, 11, 12, 13, 59, 27, 2, 317, 0)
timestamp_utc = calendar.timegm(time_tuple_utc)
print repr(timestamp_utc)

#-------------------------------------------------
# results
#-------------------------------------------------
# 2008-11-10 17:53:59
# 2008-11-12 13:51:18
# datetime.datetime(2008, 11, 12, 13, 51, 18)
# datetime.datetime(2008, 11, 10, 17, 53, 59)
# datetime.datetime(2008, 11, 12, 13, 59, 27, 595983)
# datetime.datetime(2008, 11, 12, 21, 59, 27, 595983)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 10, 17, 53, 59, 0, 315, -1)
# (2008, 11, 12, 21, 59, 27, 2, 317, 0)
# (2008, 11, 12, 13, 59, 27, 2, 317, 0)
# 1226527167.0
# 1226498367

Comments


#1 David Eaton commented on :

Hey, thanks for the cheat sheet for python date conversions, I just started learning python and this has been a great help!


#2 rgz commented on :

Thanks for the reference.

Honestly I think python's datetime library is pretty bad, consider the fact that basic datetime usage requires to import the time module and such, I understand why the mx DateTime library is so popular altough it has its downsides too.


#3 Eliot commented on :

rgz, you're welcome. I had never heard of mxDateTime but thanks for mentioning it. It does seem these libraries could be better organized. Based on a few comments in this article, we're not alone. And it appears the datetime/time libraries haven't changed in Python 3.x.


#4 Hari commented on :

Thanks for the reference, it was extremely useful. I especially missed to see the datetime.fromtimestamp() in the docs so was looking for exactly that.


#5 JJ commented on :

Thanks! I always seem to refer back to this. Another library worth mentioning is dateutil, handles fuzzy dates well in my opinion


#6 alswl commented on :

Thanks, it was helpful to me.


#7 Graham commented on :

The .fromtimestamp bit turned out to be exactly what I needed. Thanks!


#8 JC Brand commented on :

Just wanted to say, your blog has been a great help for quick references. Thanks!


#9 Walid Shaari commented on :

Thanks, I have resolved one issue i had with one of my scripts because of your above post. great work, keep it up


#10 Mike commented on :

Thanks for the reference. Helped in a pinch.


#11 sawidis commented on :

I've visited this page countless times and I've never thanked you for this great, easy-to-find-what-you-really-need cheatsheet.

After so many years, thank you =)


#12 playtyping commented on :

thanks man, perfectly clear for datetime conversion in python