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

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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? 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.
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 as you suggested. Your comments are appreciated.

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.