I need to convert date string "2013-1-25" to string "1/25/13" in python.
I looked at the datetime.strptime but still can't find a way for this.
            Asked
            
        
        
            Active
            
        
            Viewed 3.9e+01k times
        
    155
            
            
         
    
    
        FObersteiner
        
- 22,500
- 8
- 42
- 72
 
    
    
        Chamith Malinda
        
- 4,399
- 5
- 24
- 28
- 
                    1You can use dateutil.parser.parse() function which converts random strings to datetime object without specifying their input formats – Nikhil Redij Jun 13 '19 at 09:34
2 Answers
293
            I assume I have import datetime before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13".
If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13".
EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
 
    
    
        Devesh Kumar Singh
        
- 20,259
- 5
- 21
- 40
 
    
    
        eumiro
        
- 207,213
- 34
- 299
- 261
