2

Hello I am trying to take a date input from jquery UI and process it into the database as a timestamp as well as read it back. I know I probably need to use strftime but cannot figure out how to do it.

the date comes in as MM/DD/YYYY

I would like to convert it into a timestamp and also after the select queries return it to MM/DD/YYYY

BillPull
  • 6,853
  • 15
  • 60
  • 99

1 Answers1

7

To parse the string into a datetime.datetime object:

In [23]: import datetime
In [29]: datetime.datetime.strptime('9/1/2011','%m/%d/%Y')
Out[29]: datetime.datetime(2011, 9, 1, 0, 0)

Here is a little diagram describing how datetime/timetuple/timestamp conversions are done in Python:

         o------------o
         |            |  dt.datetime.utcfromtimestamp (*)
         |            |<-----------------------------------o 
         |            |                                    |
         |  datetime  |                                    |
         |            |  dt.datetime.fromtimestamp         |
         |            |<----------------------------o      |
         |            |                             |      |
         o------------o                             |      |
            |   ^                                   |      |
 .timetuple |   |                                   |      |
            |   | dt.datetime(*tup[:6])             |      |
            v   |                                   |      |
         o------------o                          o------------o
         |            |-- calendar.timegm (*) -->|            |
         |            |                          |            |
         |            |---------- time.mktime -->|            |
         |  timetuple |                          |  timestamp |
         |            |<-- time.localtime -------|            |
         |            |                          |            |
         |            |<-- time.gmtime (*)-------|            |
         o------------o                          o------------o

(*) Interprets its input as being in UTC and returns output in UTC

So, to convert it to a timestamp (regarding the input as a local datetime):

In [30]: import time

In [31]: time.mktime(datetime.datetime.strptime('9/1/2011','%m/%d/%Y').timetuple())
Out[31]: 1314849600.0

To convert it to a timestamp (regarding the input as a UTC datetime):

In [32]: import calendar

In [33]: calendar.timegm(datetime.datetime.strptime('9/1/2011','%m/%d/%Y').timetuple())
Out[33]: 1314835200

To convert it back to a string in MM/DD/YYYY format:

In [34]: timestamp=1314849600.0
In [35]: datetime.datetime.fromtimestamp(timestamp).strftime('%m/%d/%Y')
Out[35]: '09/01/2011'

or (if the timestamp is with respect to UTC):

In [36]: datetime.datetime.utcfromtimestamp(timestamp).strftime('%m/%d/%Y')
Out[36]: '09/01/2011'
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • When I run these examples, I get the error message: `NameError: name 'wckCalendar' is not defined`. I am running Python 3.4.1 on Windows 32 bit machine. Any ideas? Thanks. – sedeh Aug 14 '14 at 01:53
  • The error is saying that Python is encountering a variable named `wckCalendar` which has not been defined. Perhaps the code is referring to this [Tkinter module](http://effbot.org/zone/wcklib-calendar.htm)? Whatever the case, this error does not seem to to be related to the code I posted above, so please start a new question for further help. – unutbu Aug 14 '14 at 02:41
  • I was also coonfused about `wckCalendar` and what it has to do with running the codes above. I am not including anything else in my code apart from what is highlighted in the answers above. Just opened a new thread about this [here](http://stackoverflow.com/questions/25299371/converting-string-date-to-timestamp-in-python-3-4) as you suggested. Your comments are appreciated. – sedeh Aug 14 '14 at 03:11