Now()

Hendrik van Rooyen mail at microcorp.co.za
Thu Apr 26 08:42:14 CEST 2007


 Robert Rawlins - Think Blue wrote:
>With time depicted to the nearest second, from my background in ColdFusion
development we used to have a >datetimeformat() function that I could use as
>
>DateTimeFormat(now(), “yyyy-mm-dd HH:mm:ss”)
>
>Which would give the current time a mask.

Here is a hack that does more or less what you want:

import time

# function to return time as a string

def time_string():
    """Get the time as a string YYYY/MM/DD HH:MM:SS - with trailing space"""

    y = time.localtime() # get the current time - a tuple of ints
    t = ''           # start with empty string
    for x in y[0:6]:  # move first 6 values over
        z = str(x)      # first make a string
        if len(z) < 2:  # formatting them
            t = t + '0'  # pad to at least two chars
        t = t + z         # as string values
 u = t[0:4] + '/' + t[4:6] + '/' + t[6:8] + ' ' + t[8:10] + ':' + t[10:12] + ':'
+ t[12:14] + ' '
    return u       # done!

hth - Hendrik




More information about the Python-list mailing list