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
Related
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()
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:
How to calculate the time interval between two time strings
(15 answers)
Closed 8 years ago.
Please tell me how to get days between two dates using two variables. I have already tried using below code but it does not work
datea = datetime.strptime(finspltsix, "%Y-%m-%d")
dateb = datetime.strptime(finspltseven, "%Y-%m-%d")
myresult9 = datea - dateb
print myresult9.days
Here's an attempt to reproduce your reported problem:
from datetime import datetime
finspltsix='2014-05-11'
finspltseven='2014-09-11'
datea = datetime.strptime(finspltsix, "%Y-%m-%d")
dateb = datetime.strptime(finspltseven, "%Y-%m-%d")
myresult9 = datea - dateb
print myresult9.days
This prints -123, which is perfectly correct: datea is indeed 123 days before dateb (check a calendar if you don't think that's right!-).
So your incredibly-fuzzy assertion that "it does not work" just cannot be substantiated -- it works perfectly fine.
Please edit your Q to show your complete code (as simplified as possible!), what you expect to see, what you actually see -- IOW, please respect the stack overflow standards you explicitly agreed to as a precondition of posting here. Once you do respect this site's standards as you have agreed to do, we'll all be better placed to help!
For example, if, as per your comment, you omitted the quotes and just had e.g
finspltsix=2014-05-11
this would be computing finspltsix as the number 1998 via two subtractions, upon which of course the strptime call would raise an exception. We can't possibly tell if that's the case, as you callously, wantonly, and deliberately chose to hide exactly what problem you saw (exception traceback, strange result, whatever!), presumably in order to make it impossible for us to help you (if that was indeed your goal, you succeeded pretty well so far -- but, what a strange goal that would be!-).