2021年2月3日星期三

Convient way to convert int(yyyymmdd) to datetime object and str

using python 3.8, pandas 1.14

I have an integer value 20210126

I want to create an datetime object and str object of it.

current method:

dt = 20210126  dt_dt = datetime(year=d_i//10000, month=(d_i%10000)//100, day=d_i%100)  str_dt = dt_dt.strftime("%Y%m%d")  

Alternative:

dt = 20210126  dt_dt = datetime(year=int(str(d_i)[:3]), month=int(str(d_i)[3:5]), day= int(str(d_i)[5:])  str_dt = str(dt)  

This works fine however wondering if there is convient way.

https://stackoverflow.com/questions/66038743/convient-way-to-convert-intyyyymmdd-to-datetime-object-and-str February 04, 2021 at 10:53AM

没有评论:

发表评论