How can i check the time in Python [duplicate] - python

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

Related

Python - run script at every specific MINUTE time of the day [duplicate]

This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed last month.
I need to run a python script at specific time at EVERY minutes. I CANNOT use time.sleep to wait 60 second for it, because my script takes 8-10 seconds to run, (60 + 8 = 68 or 60+10 = 70)
I need to run the script at below specific time through out the day.
2023-01-09 01:01:05
2023-01-09 01:02:05
2023-01-09 01:03:05
2023-01-09 01:04:05
2023-01-09 01:05:05
I do lots of search and can't find a good answer. Worse case should i create a dataframe to tell the script based on the time?
besides, in window Task Scheduler,I don't see there's a option to set 1 Minute recurring.
thanks
You can calculate the time when it should next run and then do:
time.sleep(nextime - datetime.now())

How to get the current time with python [duplicate]

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()

Receiving inaccurate time from python time module? [duplicate]

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

Using Python to get current time in yyyy-mm-ddThh:mm:ss.000Z format and also add days [duplicate]

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

Disable Program at specific date [duplicate]

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

Categories