2

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!

Jonny Nguyen
  • 43
  • 1
  • 6
  • 3
    Does this answer your question? [How to get the current time in Python](https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python) – armiro Mar 28 '20 at 08:34

3 Answers3

2

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

lekshmi
  • 338
  • 3
  • 9
  • Thank you! I have another question. Let say if I want to set reminder for 2 days from current time, how can I do it? – Jonny Nguyen Mar 28 '20 at 23:52
1
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#
Shahir Ansari
  • 1,682
  • 15
  • 21
1

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)
Mohsen
  • 1,079
  • 2
  • 8
  • 23
  • Thank you! I have another question. Let say if I want to set reminder for 2 days from current time, how can I do it? – Jonny Nguyen Mar 28 '20 at 23:52
  • @Jonny Nguyen to make it too short, set you time , time.sleep, print reminder. As it is some thing completely different from your question, try your code and if there is issue just raise another question. Good luck – Mohsen Mar 29 '20 at 05:36