2

I want to know how to convert this date format

"Thu 21st Aug '14, 4:58am" 

to a timestamp with Python?

Another format that I need to convert:

"Yesterday, 7:22am"

I tried parse util without success...

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
krukita
  • 89
  • 1
  • 10

2 Answers2

7

If you haven't done so already, have a look at the parse function in dateutils.parser for parsing strings representing dates...

>>> from dateutil.parser import parse
>>> dt = parse("Thu 21st Aug '14, 4:58am")
>>> dt
datetime.datetime(2014, 8, 21, 4, 58)

...and then to convert a datetime object to a timestamp, you can do the following:

>>> import time
>>> import datetime
>>> time.mktime(dt.timetuple())
1408593480.0

As side remark, parse is a useful function which can recognise a huge range of different date formats. However it's sometimes too helpful and sees dates where perhaps a date is not intended:

>>> parse("14, m 23")
datetime.datetime(2014, 8, 23, 0, 14)

If you also want to parse expressions such as "Yesterday, 7:22am", you could do one of two things:

  1. Replace "yesterday", "yester-day", "yday" and other variations with "25/08/2014" (or another appropriate date) and then use parse on the new string.

  2. Use another library to parse the string. parsedatetime is one option...

Here's parsedatetime in action on your example:

>>> import parsedatetime.parsedatetime as pdt
>>> p = pdt.Calendar()
>>> d = p.parse("Yesterday, 7:22am")
>>> d
((2014, 8, 25, 7, 22, 0, 0, 237, -1), 3)

To turn this date representation d into a datetime object, you can unpack the tuple like so:

>>> dt = datetime.datetime(*d[0][:7])
>>> dt
datetime.datetime(2014, 8, 25, 7, 22)

Now dt can be easily converted to a timestamp in the way described above.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
  • and to create a timestamp? – Padraic Cunningham Aug 26 '14 at 13:19
  • parse util cant parse this format: "Yesterday, 7:22am" what can i do to parse this format to timestamp? – krukita Aug 26 '14 at 13:42
  • [This answer](http://stackoverflow.com/questions/1495487/is-there-any-python-library-for-parsing-dates-and-times-from-a-natural-language) recommends [parsedatetime](https://code.google.com/p/parsedatetime/). Looks promising. – Alex Riley Aug 26 '14 at 13:45
  • @user3458842 - I've updated my answer to include your new example. – Alex Riley Aug 26 '14 at 20:30
  • you could use `p.parseDT()` instead of `p.parse()`, to get `datetime()` object. – jfs Apr 10 '15 at 23:01
  • `mktime()` may return a wrong result sometimes (50% chance during a end-of-DST transition). You could use `tz = tzlocal.get_localzone(); aware_dt = tz.localize(dt, is_dst=None)`, to assert that the input time is unambiguous. And `timestamp = (aware_dt - epoch).total_seconds()`, to get POSIX timestamp, where `epoch = datetime(1970,1,1, tzinfo=pytz.utc)`. – jfs Apr 10 '15 at 23:04
0

You can use this:

 a = "Thu 21st Aug '14, 4:58am"
 datetime.datetime.strptime(a, '%a %dst %b \'%y, %H:%M%p')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RaminNietzsche
  • 2,683
  • 1
  • 20
  • 34