Feed on
Posts
Comments

get timestamp in python


to insert a timestamp into your python code:

import time

….

print time.localtime()


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

12 Responses to “get timestamp in python”

  1. on 10 May 2010 at 10:00 am A REAL python coder

    To get the timestamp from python you should do this morom.

    import time

    print time.time()

  2. on 21 Aug 2010 at 11:47 am Thanks

    Wow thanks douchenozzle. time.time() gives a /super/ useful return value…. wait, no. At least time.localtime() gives somewhat useful values.

  3. on 16 Sep 2010 at 5:21 pm Yo

    How is a unix timestamp not useful?

  4. on 20 Oct 2010 at 1:50 pm Sinik

    Real coders just get a hint where to search. And not follow blindly what they find on the net.

  5. on 27 Oct 2010 at 7:31 pm Joshua D. Burns

    Wow, you all are a bunch of socially inept pricks- Grow up, or don’t even bother replying with your worthless remarks..

    As for the answer to this question, the following is probably the best solution- Especially if you’re planning on storing the timestamp possibly to be interacted with by other services or languages:

    # Output: 1288221999.43
    import time
    print time.time()

    Otherwise, here are some other solutions as well:

    # Output: 2010-10-27 19:29:48.401560
    import datetime
    print datetime.datetime.now()

    # Output: 2010-10-27
    import datetime
    print datetime.date.today()

  6. on 14 Feb 2011 at 1:01 pm Skip Huffman

    Thanks for the helpful remarks Joshua.

  7. on 08 Jun 2011 at 8:45 pm tony

    Okay, so, why is it if I do something like, say

    import time
    timenow = time.time().tm_hour, time.time().tm_min, time.time().tm_sec
    print timenow
    I get
    (11, 45, 31)
    for instance,
    and a minute later, when I do
    print timenow
    I get
    (11, 45, 31)
    as if the clock has stopped.

    These datetime, and time functions in python are striking me as nearly useless…Maybe I’m spoiled, but in bash, tcl, or php, when I tell a script to give the time, I can get them to give the time at the moment when I ask it to, not the time 5 minutes ago when it was first asked.
    They will all also print the time in a human readable fashion with relatively little effort.
    (bash – date -d %I:%M:%S
    php – date(h.i.s), etc.)

    I don’t know wny ESR says python is a good, easy place to start. Tcl was a lot simpler.

  8. on 29 Sep 2011 at 6:02 am Ice

    Ugh… Tony, if you store something in a variable, the variable isn’t going to change by itself. time() returns the time as it is the moment the function is called. You need to call the time() function again to get the current time value. In PHP, if you store date(“H:i:s”); return value in $date and print it out as many times as you’d like, the value printed will be the same, too.

    You probably didn’t bother to look at the python’s time or datetime module docs, since you would’ve most likely noticed that there’s an equivalent for “date -d %I:%M:%S” and date(“H:i:s”) there: time.strftime(“%H:%M:%S”). Similar method exists for the datetime object, if you wanted to use it instead.

    Also, remember that in python you can always decide the name under which something is imported into the local namespace. So you could do something like the example below, if remembering and/or typing out strftime seems awkward to you.

    from time import strftime as date
    print date(‘%Y-%m-%d %H:%M:%S’)

  9. on 31 May 2012 at 5:25 am mizar

    Tony, i hope you were kidding. time.time() is returning a float, not a time Object reference. this is a base concept for every oop language.

  10. on 02 Jul 2012 at 5:34 pm Jordan

    This thread is absolutely HILARIOUS!

    #Output is “hilarious”

    import python
    print(“hilarious”)()()()()()

  11. on 25 Oct 2012 at 7:58 pm Cody

    Timestamps predate objects, Tony was correct. All of these entries are technically timestamps.

  12. on 18 Apr 2016 at 1:32 am regwgre

    Jordan, you have nothing better to say, so just keep your quips to yourself.

    sudo apt-get install “IM SO EDGY HURR DURR I FIT IN”
    Just stop

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.