just wondering if there is anything in python to give the date automaticly, just like excel has =today()

cheers.

Recommended Answers

All 15 Replies

You should try using the time module

import time
print time.localtime()

cheers mate.

it gives me a returen of
"(2008, 6, 4, 19, 11, 46, 2, 156, 0)"
which is Year, month, day, hour, min, seconds, something, something, someting
how do i get only 24hour time, so i can get 19.11

you can use the following code:

time.localtime()[3:5]

that just gives the hour and the minutes

mate your a legend.
is it possibe to subtract one time from another?

i want to say,
19 , 32 take 19, 22
and it will leave me with the answer
10

mate i appreciate it.

you could do it by making it into the amount of minutes. This way you can make sure it stays base 60
TO get the difference in minutes:

g = time.localtime()[3:5]
f = time.localtime()[3:5]
m = g[0]*60+g[1]
n = f[0]*60+f[1]
print n-m

to make it back into hours and minutes:

g = time.localtime()[3:5]
f = time.localtime()[3:5]
m = g[0]*60+g[1]
n = f[0]*60+f[1]
g=n-m
hours = g/60
g=g-hours*60
mn = g
print hours,":",mn

I think that works

LEGEND!!!

got another one for ya mate.
same thing but for the date? to give me dd/mm/yy?
same as =today() in excel.
cheers.

well im at it.
sorry to be a pain, but still learning.. and loving it, your helping majorly!!

i have two more issues, and than i've finished it.
how can i make it, instead of "press enter to exit" that you press enter to go back to the top of the program and run again?
cheers again!

alright how about this:

import time
day = time.localtime()[2]
month = time.localtime()[1]
year = time.localtime()[0]
print day,'/',month,'/',year

This does not show the name of the day but the date in the same format as dd/mm/yy

you can do a

While True:
    ##insert program
    print "press enter to restart"
    raw_input()

OR

exec file(filename here)

mate, i owe you a beer.. or 5!
last one.. i promise lol
we are taking all of our inputs and saving them to a text file, however, it saves without the .txt extention.
any ideas?
cheers.

You use a while loop like that to exit or stay in the program:

while True:
    # ...
    # you code here
    # ...
    sel = raw_input("Enter q to quit, m for more: ").lower()
    # entering q will break the loop, anything else keeps looping
    if sel == 'q':
        break

got another one for ya mate.
same thing but for the date? to give me dd/mm/yy?
same as =today() in excel.
cheers.

The module time has all the methods you need to accomplish this:

import time
today = time.strftime("%d/%m/%y", time.localtime())

print today  # eg. --> 04/06/08

ah heres a quick idea.

f = file(filename+".txt",'w')
in = raw_input(">")
f.write(in+"\n")
f.close()

This will open a file with the extention of .txt and then it will get a raw_input and write it to a text file and then add a new line and then close the file.
If what you are trying to write an integer to a text file make sure to do this:

f.write(str(integer here))
f.close()

yeah this might not be the fastest way but its the best i can do.

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.