当前位置:网站首页>AttributeError: ‘str‘ object has no attribute ‘strftime‘

AttributeError: ‘str‘ object has no attribute ‘strftime‘

2022-07-08 01:24:00 Bosong

from datetime import datetime, date

now_date = '2021-6-24 15:23:29.000227'
nowdate = now_date.strftime(now_date,"%m/%d/%Y")

    Want to get only... From the moment represented by a string “ year / month / Japan ”, But running the above code will report an error 'str' object has no attribute 'strftime'.

    The improvement method is as follows :

now_date = datetime.strptime(now_date, '%Y-%m-%d %H:%M:%S.%f')
now_date = now_date.strftime("%m/%d/%Y")

    We need to use it first. .strptime() take string Format variables into datetime Time format under , And then reuse it .strftime() obtain “ year / month / Japan ”.

PS: If you want to get the day of the week today ,datetime.strptime().weekday() + 1

原网站

版权声明
本文为[Bosong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130542271745.html