Few lines of code to show all the basics of python datetime handling

Here is a few lines of code to show :
Line 3. How to Create a datetime object on a certain time
Line 4. How to subtract or add time to another time and how to compare datetime objects
Line 5. How to convert datetime to string
Line 6. How to get the current time

import datetime

lasttime = datetime.datetime(1900,1,1,0,0,0)

while True :
    if lasttime  < datetime.datetime.now() - datetime.timedelta(seconds=30) :
        print("30 Secornds has passed Date Now is :" + datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p") )
        lasttime = datetime.datetime.now()

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image