Parsing & Formatting Date/Time in Python

Discussion in 'Python' started by pradeep, Jun 7, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Being in the era of instant updates & notifications one would at some point or the other would need to parse/format date & time, be it any language. Here, we'll see how we can parse & format date/time with Python.

    Python is natively equipped with the functions to make our job easier, the function we would look at are strftime() & strptime(), both functions are available in the Python package time.

    Parsing Date/Time



    strptime(string[, format]) requires the date/time string as the first argument and the second argument (optional, it defaults to "%a %b %d %H:%M:%S %Y") is the format string and returns a time_struct object. Go through the example code below for a demo.

    Code:
    #!/usr/bin/python
    
    import time
    
    t = time.strptime('2 Jun 2012 11:15:29','%d %b %Y %H:%M:%S')
    
    print "parsed time_struct: %s " % t
    
    ## this will print year
    print t[0]
    
    Here's the output:
    Code:
    parsed time_struct: (2012, 6, 2, 11, 15, 29, 5, 154, -1)
    2012
    
    Now, you may work with the returned object and use the required data in it, like you may only need the year as I've used in the example.

    Formatting Date/Time



    strftime(format[, t]) outputs the data passed as time_struct object into the format specified, if t is not passed the current value that'd be returned by localtime() will be used. Again, a code example to play with & understand.

    Code:
    #!/usr/bin/python
    
    import time
    
    start_t = time.localtime();
    
    print time.strftime('Started : %d %b %Y %H:%M:%S',start_t)
    
    ## imitate some work, let's sleep
    time.sleep(5)
    
    end_t = time.localtime();
    
    print time.strftime('Ended : %d %b %Y %H:%M:%S',end_t)
    
    ## incase you have an unix timestamp
    unix_timestamp = 1339074642
    
    unix_t = time.localtime(1339074642)
    
    print time.strftime('Unix TS : %d %b %Y %H:%M:%S',unix_t)
    
    Here we have just touched the basics of date/time formatting, let the internet be your guide. Happy coding!!

    Format Strings For strftime/strptime




    • %a => Locale’s abbreviated weekday name.
    • %A => Locale’s full weekday name.
    • %b => Locale’s abbreviated month name.
    • %B => Locale’s full month name.
    • %c => Locale’s appropriate date and time representation.
    • %d => Day of the month as a decimal number [01,31].
    • %H => Hour (24-hour clock) as a decimal number [00,23].
    • %I => Hour (12-hour clock) as a decimal number [01,12].
    • %j => Day of the year as a decimal number [001,366].
    • %m => Month as a decimal number [01,12].
    • %M => Minute as a decimal number [00,59].
    • %p => Locale’s equivalent of either AM or PM.
    • %S => Second as a decimal number [00,61].
    • %U => Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
    • %w => Weekday as a decimal number [0(Sunday),6].
    • %W => Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
    • %x => Locale’s appropriate date representation.
    • %X => Locale’s appropriate time representation.
    • %y => Year without century as a decimal number [00,99].
    • %Y => Year with century as a decimal number.
    • %Z => Time zone name (no characters if no time zone exists).
    • %% => A literal '%' character.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice