简体   繁体   English

datetime.date(TimeStamp).replace(day = 01)给出整数是必需的错误

[英]datetime.date(TimeStamp).replace(day=01) gives an integer is required error

I have a timestamp column in my Db table (defined as datetime column) and I try to make the day value as first of that month . 我的Db表中有一个timestamp列(定义为datetime列),我尝试将day值设为该月的第一天。

for (ObservationRawDataId, TimeStamp, TankSystemId, RawDeliveryLitres, ProductName, SiteCode) in cursor:
    print TimeStamp <----2019-06-21 00:00:00

     startdate = datetime.date(TimeStamp).replace(day=01)

But im getting following error; 但是我得到以下错误;

 an integer is required

What Im doing wrong here? 我在这里做错了什么?

You need to parse the date string to date object using datetime.strptime . 您需要使用datetime.strptime将日期字符串解析为date对象。 You can only work with datetime object here startdate.replace(day=1) 您只能在此处使用datetime对象startdate.replace(day=1)

from datetime import datetime
startdate = datetime.strptime("2019-06-21 00:00:00","%Y-%m-%d %H:%M:%S").replace(day=1)
print(startdate)

Output: 输出:

2019-06-01 00:00:00

See this in action here 这里看到这个动作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM