开发者

How to convert a time to a string

开发者 https://www.devze.com 2023-02-07 23:12 出处:网络
I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated

I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated

from datetime import date
today=date.today()
prin开发者_C百科t today


You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:

from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'


datetime.strftime allows you to format a datetime however you want


stringDate = str(today)

If that's what you want.


In Python3, you can use f-string formatting:

from datetime import date 
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号