-1

I have to convert this- ['Fri May 29 20:47:53 2020'] datetime format to date format YYYY-MM-DD

How do I achieve it in Python.

I have tried this-

import os
import time
print(time.ctime(max(os.path.getmtime(root) for root,_,_ in os.walk('/dbfs/mnt/datalake/.../'))))

Which gives a date like ['Fri May 29 20:47:53 2020'] and I have to convert it to YYYY-MM-DD.

Can I achive this by simply modifying above code or any other way please suggest.

Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30

4 Answers4

2

You should start from os.path.getmtime() that returns standard timestamp:

>>> import os
>>> os.path.getmtime('y1')
1451122262.0
>>> import datetime
>>> dt = datetime.datetime.fromtimestamp( os.path.getmtime('y1'))
>>> dt
datetime.datetime(2015, 12, 26, 18, 31, 2)
>>> dt.strftime('%Y-%m-%d')
'2015-12-26'
>>> 
lenik
  • 23,228
  • 4
  • 34
  • 43
1

You can use datetime.strptime with the format of the string you used
%a is for the local shortened day name (if you have full name of day use %A)
%B is for month name
%d is for date
%H is for Hour
%M is for month
%S is for second
%Y is for year

import os
import time
from datetime import datetime
dat =['Fri May 29 20:47:53 2020']
for i in dat:
    print(datetime.strptime(i,'%a %B %d %H:%M:%S %Y'))

Output:
2020-05-29 20:47:53
DaVinci
  • 868
  • 1
  • 7
  • 25
0

I think you can try this,

from datetime import datetime

datetime.now().strftime('%Y-%m-%d')
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Mustafa Kemal
  • 762
  • 1
  • 5
  • 11
0

This resolved my issue-

import os
import time
from datetime import datetime
dat =['Fri May 29 20:47:53 2020']
for i in dat:
    print(datetime.strptime(i,'%a %B %d %H:%M:%S %Y').date())
Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30