Changing date format in output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deathal
    New Member
    • Sep 2008
    • 1

    Changing date format in output

    Hi I'm trying to make a list of dates between two dates and i found this

    http://bytes.com/forum/thread774887.ht ml

    and used the code at the bottom to make the list but i need the list to output in mm/dd/yy format instead of yyyy-mm-dd, and i was wondering if there is a line or two of code i could add to have it do that for me.

    I'm pretty new to python so if this is a stupid question with a really easy answer sorry for wasting peoples time.

    Thank you for any help.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    String formatting makes this fairly easy (and fun!).

    Code:
    instr = "2008-09-10"
    spl = instr.split('-') #(2008, 09, 10)
    outstr = "%d%d/%d%d/%d%d" % (int(spl[1][0]), int(spl[1][1]), int(spl[2][0]), int(spl[2][1]), int(spl[0][2]), int(spl[0][3]))

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You can use date object method strftime() to format the output from the function in the referenced thread.
      Code:
      print '\n'.join([date.strftime('%m/%d/%y') for date in dateList])

      Comment

      Working...