This question already has answers here:
How do I get the current time?
(54 answers)
Closed 2 years ago.
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.
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"))
You can use
import datetime
my_time = datetime.datetime.now().time()
Related
This question already has answers here:
How do I get the current time?
(54 answers)
Closed 3 years ago.
I want to get the Date and Time of The Current Location. If somebody runs the code in a different country thought it will show the date and and time for them, and not for me. If you know any commands for this please put the down bellow. I prefer If I don't need a library so that people who get the code don't need to download the library.
I think you can not get the date or time of a location.
This code gets your device's date and time:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")
print("Current Time: ", current_time)
print("Current Date: ", current_date)
A standard Python module called datetime can probably help you with retrieving the current system date and time from where the program is run
Since it's a standard module, there would be no need to install it rather just import it in the program itself.
Example :
import datetime
x = datetime.datetime.now()
print(x)
Would give you the output as :
2019-07-20 00:52:27.158746
The above output contains the current year, month, day, hour, minute, second, and microsecond.
This question already has answers here:
Python: strftime, gmtime not respecting timezone
(2 answers)
Closed 3 years ago.
I am a beginner in programming and I was messing around with the time module in python, as I recently got a raspberry pi and want to start doing some projects. I imported the time module, and just made a loop to print out the time every second. The code runs correctly, but the time given is not accurate to my location. Currently, it is the 14th and a Friday, around 9 pm, but it is returning the 15th a Saturday, with 0 hours and 10 minutes. Does anyone know how I can obtain the correct time?
I tried a couple of the different functions to receive the current time like .localtime() and .gmtime() but they're all the same.
import time
while(True):
thisTime = time.gmtime()
print(time.asctime(thisTime))
time.sleep(1)
Go ahead and check out this post, I think this is your solution:
Python get current time in right timezone
So assuming your computer's time is configured correctly, you could use this:
from datetime import datetime
naive_dt = datetime.now()
It's unclear what else you've tried, although the following should work:
localtime = time.localtime()
print(time.asctime(localtime))
↳ https://docs.python.org/3/library/time.html#time.localtime
This question already has answers here:
Python script to do something at the same time every day [duplicate]
(4 answers)
Closed 5 years ago.
How can i check the time in Python and run a command every day at the same time?
if datetime.time() == 17.30:
bot.sendMessage(ChatID, "Message")
I found a lot in the Internet, but i didn't found something
which really helped me. I like to check the hour and the minutes.
Later it should be implimented into a TelegramBot, which should send a Message to a Group every day. So Crontab would not be so nice also a timer.
Thanks for helping
This is probably better handled with a cron job, but here's the code
import datetime.datetime
now_time = datetime.now()
If now_time.hour == 17 and now_time.minute == 30:
#do something
This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 5 years ago.
I'm using Python and I need to get the time in a certain format that this API uses. So I need to get current time in the format of yyyy-mm-ddThh:mm:ss.000Z and then also create a new date in the future by adding a number of days to it.
So if I could get the current time of 2017-08-19T07:00:00.000Z and add 30 days to it to get 2017-09-18T07:00:00.000Z. What would be the easiest way to write this in Python?
Current date
import datetime
now = datetime.datetime.now()
format_iso_now = now.isoformat()
Add 10 days
from datetime import timedelta
later = now + timedelta(days=10)
format_later_iso = later.isoformat()
Output
print(format_iso_now, format_later_iso)
2017-08-20T02:43:07.177167 2017-08-30T02:43:07.177167
And to match your needs
print(now.strftime('%Y-%m-%d %H:%M:%S:%fZ'))
2017-08-20 02:48:00:103856Z
More details on documentation
This question already has answers here:
Checking date against date range in Python
(5 answers)
Closed 8 years ago.
So I would like to write a program that disables itself on let's say May 1st.
How would I do that? I was thinking about getting the time with localtime and then writing a "if block" to ask the program if the date is >= to the May 1st. If so it should prompt the user that he can not use the program any longer cause it expired.
But I have a problem writing the if statement, since localtime returns so many values. How would I write the if block?
What you are looking for is to convert datetime to date which you can do by datetime.datetime.now().date(), for the if condtion you can do something like this:
THis is just an example:
import datetime
if datetime.datetime.now().date() >= datetime.date(2012, 1, 15): #insert your date here
print "True"
...........
Your Code
...........
All the Documentation you would ever want on this topic is located here, http://docs.python.org/2/library/datetime.html