我相信我需要將日期時間轉換為字串,以便我可以將這個變數插入到我的線性方程中
now = datetime.datetime.now()
x = datetime.datetime.strptime(now.astype(str).str[:10], '%Y-%m-%d').toordinal()
我收到此錯誤:
型別錯誤:strptime() 引數 1 必須是 str,而不是 datetime.datetime
或者
ValueError:未轉換的資料仍然存在:18:17:27.424685 當我嘗試時
x = datetime.datetime.strptime(str(now), '%Y-%m-%d').toordinal()
或者
AttributeError: 'datetime.datetime' 物件在我嘗試時沒有屬性 'astype'
x = datetime.datetime.strptime(now.astype(str).str[:10], '%Y-%m-%d').toordinal()
uj5u.com熱心網友回復:
strptime 是一個將字串轉換為日期時間物件的函式,您已經有一個日期時間物件,并且您正在嘗試將其轉換為字串并回傳到日期時間物件,這是不需要的。
我的猜測是,您正在嘗試洗掉 datetime 物件的時間組件以獲取日期的序號,您可以更輕松地這樣做:
now.date().toordinal()
但是由于toordinal()無論如何都忽略時間分量,因此執行以下操作將產生相同的結果:
now.toordinal()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338589.html
