date reformatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bell, Kevin

    #1

    date reformatting


    Anyone aware of existing code to turn a date string "8-15-05" into the
    number 20050815?

    The dateutil module has a parse method that looks perfect, I downloaded
    and unzipped it, but could figure out how to install it. I using
    windows XP and py2.4.

    Any ideas?


    Kev

  • elbertlev@hotmail.com

    #2
    Re: date reformatting

    setup.py install

    Comment

    • Larry Bates

      #3
      Re: date reformatting

      There's more than one way, but this one will work for
      dates prior to 2000.

      import time
      datestring='8-15-05'
      d=time.strptime (datestring,'%m-%d-%y')
      number=10000*d[0]+100*d[1]+d[2]
      print number

      -Larry Bates

      Bell, Kevin wrote:[color=blue]
      > Anyone aware of existing code to turn a date string "8-15-05" into the
      > number 20050815?
      >
      > The dateutil module has a parse method that looks perfect, I downloaded
      > and unzipped it, but could figure out how to install it. I using
      > windows XP and py2.4.
      >
      > Any ideas?
      >
      >
      > Kev
      >[/color]

      Comment

      • Larry Bates

        #4
        Re: date reformatting

        There's more than one way, but this one will work for
        dates prior to 2000.

        import time
        datestring='8-15-05'
        d=time.strptime (datestring,'%m-%d-%y')
        number=10000*d[0]+100*d[1]+d[2]
        print number

        -Larry Bates

        Bell, Kevin wrote:[color=blue]
        > Anyone aware of existing code to turn a date string "8-15-05" into the
        > number 20050815?
        >
        > The dateutil module has a parse method that looks perfect, I downloaded
        > and unzipped it, but could figure out how to install it. I using
        > windows XP and py2.4.
        >
        > Any ideas?
        >
        >
        > Kev
        >[/color]

        Comment

        • Magnus Lycka

          #5
          Re: date reformatting

          Bell, Kevin wrote:[color=blue]
          > Anyone aware of existing code to turn a date string "8-15-05" into the
          > number 20050815?[/color]
          [color=blue][color=green][color=darkred]
          >>> import datetime
          >>> s = "8-15-05"
          >>> month,day,year = map(int, s.split('-'))
          >>> date = datetime.date(2 000+year,month, day)
          >>> date.strftime(' %Y%m%d')[/color][/color][/color]
          '20050815'

          Of course, if you really want the *number* 20050815 you'd
          have to do[color=blue][color=green][color=darkred]
          >>> int(date.strfti me('%Y%m%d'))[/color][/color][/color]

          Using a datetime.date object means that you have good
          support for a lot of arithmetic on and formatting of
          dates without writing a lot of new code.

          If you really mean that you want the number 20050815, I
          assume this is because some legacy system beyond you control
          need to have dates in that format. It's not a particularly
          good format for dates. If you just want a numeric storage of
          dates, I'd suggest using datetime.date.t oordinal/fromordinal.
          Those numbers aren't as easy to decipher manually, but at
          least they work right if you subtract dates or add or subtract
          days from a date.

          Comment

          Working...