Jonny Nguyen
Jonny Nguyen

Reputation: 43

Real Time Clock(RTC) in Python

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!

Upvotes: 2

Views: 9119

Answers (3)

Shahir Ansari
Shahir Ansari

Reputation: 1848

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#

Upvotes: 1

Mohsen
Mohsen

Reputation: 1089

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)

Upvotes: 1

lekshmi
lekshmi

Reputation: 348

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 ...

Upvotes: 2

Related Questions