开发者

Converting String Date to Timestamp and back python

开发者 https://www.devze.com 2023-04-02 08:12 出处:网络
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 can开发者_JAVA技巧not figure o

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 can开发者_JAVA技巧not 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


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消