1

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.

  • 1
    Have you looked at the documentation for the [`datetime` module](https://docs.python.org/3/library/datetime.html)? – Mark Ransom Oct 13 '20 at 18:28
  • 2
    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) – Metropolis Oct 13 '20 at 18:32

2 Answers2

3
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
  • 150
  • 9