Real Time Clock(RTC) in Python

Viewed 4380

I am a beginner in Python. I am wondering about does Python has Real Time Clock which is giving us exactly year, month, day, hour, min, and second?

Thank you for your help!

3 Answers

For Date Time operations in python you need to import datetime library.There different functions related to date formatting, getting current date and time etc.. Refer this documentation

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Hope the above code will do your job ...

datetime.now()

Thats it.It gives the current date and time upto microseconds. Use as you want.

Setting an alarm after 2 days.

alarm_time = date.today() + timedelta(days = 2)     #set alarm time 2day after today

while(date.today()!=alarm_time):                    #run loop till datetoday is not  alarm_time
    time.sleep(60*60)                               #wait for an hour then recheck time
#do here anything you want to do when alarm time has reached#

you can try this:

temp.year, temp.month, temp.day,temp.hour,temp.minute,temp.second would give your desire values separately

from datetime import datetime

temp = datetime.now()
timee = "{:04d}{:02d}{:02d} {:02d}:{:02d}:{:02d}".format(temp.year, temp.month, temp.day,temp.hour,temp.minute,temp.second)
Related