Search code examples
pythonpython-datetime

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 .

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?


Solution

  • You need to parse the date string to date object using datetime.strptime. You can only work with datetime object here 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