3

How do you get the current time in python? I have seen people get the date and time, but I only want the time as a string.

For example, if the current time was 8:30, the command should output: "8:30".

Also, how do you find what day of the week it is>

For example, if it is Tuesday hen the output should be: 2.

Kromydas
  • 111
  • 3
  • 11

2 Answers2

7
from datetime import datetime

print(datetime.now().strftime("%H:%M"))

Will return this:

13:29

To get seconds too:

print(datetime.now().strftime("%H:%M:%S"))
1

You can use

import datetime
my_time = datetime.datetime.now().time()
MD98
  • 344
  • 2
  • 9