Search code examples
pythondatetime

How can I fix datetime error when trying to use the now syntax?


I am trying to run the code below but when I do, I keep getting errors, i managed to change the import syntax from

import datatime as dt

to

from datetime import date time as dt

This has worked but I am not getting the exact time I need. This is what I got instead

Timestamp('2024-07-04 00:00:00')

How can I fix this?


Solution

  • If you just want to get today's date without hours / minutes / seconds, you can use:

    from datetime import date
    
    date.today()
    >>> datetime.date(2024, 7, 4)
    

    Alternatively for right now:

    from datetime import datetime
    
    datetime.now()
    >>> datetime.datetime(2024, 7, 4, 12, 46, 30, 841567)