Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 11th, 2008, 02:34 AM
Newbie
 
Join Date: Sep 2008
Posts: 1
Default 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.html

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.
Reply
  #2  
Old September 11th, 2008, 03:26 AM
Expert
 
Join Date: Sep 2007
Posts: 846
Default

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

Expand|Select|Wrap|Line Numbers
  1. instr = "2008-09-10"
  2. spl = instr.split('-') #(2008, 09, 10)
  3. 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]))
  4.  
Reply
  #3  
Old September 11th, 2008, 01:57 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,214
Default

You can use date object method strftime() to format the output from the function in the referenced thread.
Expand|Select|Wrap|Line Numbers
  1. print '\n'.join([date.strftime('%m/%d/%y') for date in dateList])
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles