Hello friends.
I want to create a countdown program for the new year that is comming.
I know i can use this code for example, for short certain time:

from threading import Timer

def sayhi():
    print "hello everybody!"

t = Timer(10, sayhi)
t.start()

But the new year begins in 29 days (2015, march 21)(at 02:15:11 AM). So i cant calculate how many seconds is left!!

So what can i do? I think i should give the date to the program and it should use a calendar maybe, but i don't know how.

Recommended Answers

All 13 Replies

oops! This is really bad that we are not able to edit title or our posts! Why this forum is like that?!
I wanted to typ "creating countdown for new year begining".

You can only edit within one hour after posting.

Try this ...

import datetime as dt

now = dt.datetime.today()
year = now.year
# Persian new year march 21 at 2:15:11
pnewy = dt.datetime(year, 3, 21, 2, 15, 11)
differ = pnewy - now
print("There are {} seconds till Persian New Year".format(differ.seconds))

hi @vegaseat , I think to get the seconds , we have to use total_seconds() and converting that to days we get the actual result

#seconds till persian new year

import datetime as dt

now = dt.datetime.today()
year = now.year
# Persian new year march 21 at 2:15:11
pnewy = dt.datetime(year, 3, 21, 2, 15, 11)
differ = pnewy - now
print("There are {} seconds till Persian New Year".format(differ.seconds))
print(differ.total_seconds())

or better yet , just use the code below instead of two print statements and it will give you the days , hours and minutes

print(differ)

Well, @fonzali, at the moment i tryed your code, the output was:

2015-02-24 07:21:53.769512
2015
2015-03-21 02:15:11
24 days, 18:53:17.230488
There are 67997 seconds till Persian New Year
2141597.23049

I've printed all variables in the code to see them in terminal; look at those 2 last lines. With .format(differ.seconds) the output was 67997 seconds, but with differ.total_seconds() the output was 2141597.23049. So why the results were different?! Why i've got 2 different seconds?!

Thank you @vegaseat. Of course the code you typed calculate seconds, but not counting down, but it's ok, i can do it myself, the code was helpful.

hi @Niloofar24 the last line gives you total seconds from the time you ran the code till new year, if you convert the line 4 to seconds you will get (24243600)+(183600) +(5360) + 17= 2141597 which is exactly what line 6 gives you .now if you convert only the hours and minutes and seconds of line 4 it gives you exactly the number in line 5 : (186060) + (53*60) + 17=67997 . to make it short , line 5 does not consider the days left , it only calculates the hours and minutes and seconds converted to seconds but line 6 gives you the actual time left converted in seconds .

hi @fonzali and thank you for explanation.
So .format(differ.seconds) calculate only 18:53:17.230488 and doesn't attention to the days, correct? But differ.total_seconds() calculate days too, right?

@vegaseat, i have a question.
With this:

delta = datetime.datetime(2015, 9, 13, 3, 5) - datetime.datetime.now()
print delta

I will get this: 200 days, 18:37:08.889568

And then with this:

hour_string_2 = str(delta).split(', ')
print hour_string_2

I will get this: ['200 days, 18:37:08.889568']

I tryed this too:

hour_string_3 = str(delta).split()
print hour_string_3

And i got this: ['200', 'days,', '18:37:08.889568']

Why? What , does exactly do in .split(', ')?
Why i get 3 indexes in the list with .split() but i get 2 indexes in the list with .split(', ')?

And with this:

hours = hour_string.split(':')[0]
print hours

I get this: 18
But with this:

hours = hour_string.split(':')
print hours

I get this: ['18', '19', '45.495552']

So it means divid everything we have in the string 200 days, 18:37:08.889568 to indexes of the list according to : sign, means split words where ever meet : sign on the string, correct?!

hi @Niloofar24 , as far as my post is concerned , yes , you got my point right , your other questions regarding split() , if you just use plain split() , it splits on white spaces and if you use anything else , also called separator , it splits on that separator . I know @vegaseat has lots of examples of it in the forum ,in starting python and his code snippets , just search for them .

I understood, thank you @fonzali.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.