parse dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brechmos

    parse dates

    Hi,

    I have been using PHP the last while and in particular strtotime.
    What I want to replicate is finding the second or fourth Monday of the
    next month. In PHP with strtotime it is easy (strtotime("sec ond
    Monday", strtotime("next month"), but I can't find an easy way to do
    it in Python. I have seen DateUtil, but it seems to be able to do
    only the simpler parsing (could be wrong).

    Any other ideas?
  • Sebastian 'lunar' Wiesner

    #2
    Re: parse dates

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    [ brechmos <brechmos@gmail .com]
    Hi,
    >
    I have been using PHP the last while and in particular strtotime.
    What I want to replicate is finding the second or fourth Monday of the
    next month. In PHP with strtotime it is easy (strtotime("sec ond
    Monday", strtotime("next month"), but I can't find an easy way to do
    it in Python. I have seen DateUtil, but it seems to be able to do
    only the simpler parsing (could be wrong).
    >
    Any other ideas?
    If parsing is not required, dateutil is just fine:

    from datetime import datetime
    from dateutil import relativedelta

    # second monday
    datetime.now() + relativedelta.r elativedelta(da y=1, weekday=relativ edelta.MO(+2))

    # next month
    datetime.now() + relativedelta.r elativedelta(mo nths=+1)

    - --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)

    iEYEARECAAYFAkh BqcMACgkQn3IEGI Lecb7FXgCgg1X7v rP/uzTaPa5W3e2WsDF V
    e5kAnizMQUDLfz0 7Z/d1hVehlCmoJuKl
    =yi9t
    -----END PGP SIGNATURE-----

    Comment

    • Eddie Corns

      #3
      Re: parse dates

      brechmos <brechmos@gmail .comwrites:
      >Hi,
      >I have been using PHP the last while and in particular strtotime.
      >What I want to replicate is finding the second or fourth Monday of the
      >next month. In PHP with strtotime it is easy (strtotime("sec ond
      >Monday", strtotime("next month"), but I can't find an easy way to do
      >it in Python. I have seen DateUtil, but it seems to be able to do
      >only the simpler parsing (could be wrong).
      >Any other ideas?

      >>import parsedatetime.p arsedatetime as pdt
      >>p = pdt.Calendar()
      >>p.parse('ne xt month')
      ((2008, 7, 1, 9, 0, 0, 1, 183, -1), 1)

      Eddie

      Comment

      Working...