Python date conversion

I have dates MMDDYY, that is today111609

How do I convert this to 11/16/2009, in Python?

+3
source share
4 answers

I suggest the following:

import datetime
date = datetime.datetime.strptime("111609", "%m%d%y")
print date.strftime("%m/%d/%Y")

It converts 010199to 01/01/1999and 010109to 01/01/2009.

+15
source
date = '111609'
new_date = date[0:2] + '/' + date[2:4] + '/' + date[4:6]
0
source
>>> s = '111609'
>>> d = datetime.date(int('20' + s[4:6]), int(s[0:2]), int(s[2:4]))
>>> # or, alternatively (and better!), as initially shown here, by Tim
>>> # d = datetime.datetime.strptime(s, "%m%d%y")
>>> d.strftime('%m/%d/%Y')
'11/16/2009'

, strptime(), , , , , 20- 21- .

0

,

11/16/2009

date_var = ("11/16/2009")    
datetime.date.replace(year=2009, month=11, day=16)
-1

Source: https://habr.com/ru/post/1723041/


All Articles