Dates in python

Asked

Viewed 53 times

0

How can I get multiple different dates in the same run as my script? I got my script done this way:

from datetime import datetime, timedelta
Time = '{:%H:%M:%S}'.format(datetime.now() + timedelta(hours=6))

But the script calls this string more than once but instead of seeing different results it always comes the same. It is possible to ensure that different results always come?

1 answer

1


But Time is a variable (by the way, python variables, by convention, should use lower case letters), hence its content will always have the same value (in this case always the same string defined at the beginning of the programme).

What you need to do is create a function for this, something like:

def time_plus_six_hours():
    return "{:%H:%M:%S}".format(datetime.now() + timedelta(hours=6))

And call her whenever necessary...

print(time_plus_six_hours())
  • 1

    Why do they must start with lower case letter?

  • Oops, correcting... missed the nay in the sentence.

  • It was even stranger the statement :S It is convention to leave the variables with lower case letters, but this is not mandatory, so the "should"

  • Improved wording.

  • I finally figured it out on my own !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.