How can I know if a date is prior to today?

Discussion in 'Python' started by Giampaolo Rodola', Mar 25, 2009.

  1. Hi,
    I have a date expressed in seconds.
    I'd want to pretty print it as "%H:%M" if the time refers to today and
    "%b%d" (month, day) if it's of yesterday or before.

    I managed to do that with the code below but I don't like it too much.
    Is there a better way to do that?
    Thanks in advance.


    import time

    today_day = time.strftime("%d", time.localtime(time.time()))
    mytime = time.localtime(time.time() - (60*60*30)) # dummy time prior
    to today
    if time.strftime("%d", mytime) == today_day:
    print time.strftime("%H:%M", mytime)
    else:
    print time.strftime("%b%d", mytime)


    --- Giampaolo
    http://code.google.com/p/pyftpdlib
    http://code.google.com/p/psutil
    Giampaolo Rodola', Mar 25, 2009
    #1
    1. Advertising

  2. Giampaolo Rodola'

    Paul McGuire Guest

    On Mar 25, 8:31 am, "Giampaolo Rodola'" <> wrote:
    > Hi,
    > I have a date expressed in seconds.
    > I'd want to pretty print it as "%H:%M" if the time refers to today and
    > "%b%d" (month, day) if it's of yesterday or before.
    >


    Use datetime module.

    import time
    from datetime import datetime
    now = time.time()
    thisTimeYesterday = now - 24*3600

    print datetime.fromtimestamp(now) < datetime.today()
    print datetime.fromtimestamp(thisTimeYesterday) < datetime.today()


    -- Paul
    Paul McGuire, Mar 25, 2009
    #2
    1. Advertising

  3. Giampaolo Rodola'

    Tim Chase Guest

    > I have a date expressed in seconds.
    > I'd want to pretty print it as "%H:%M" if the time refers to today and
    > "%b%d" (month, day) if it's of yesterday or before.
    >
    > I managed to do that with the code below but I don't like it too much.
    > Is there a better way to do that?
    > Thanks in advance.
    >
    >
    > import time
    >
    > today_day = time.strftime("%d", time.localtime(time.time()))
    > mytime = time.localtime(time.time() - (60*60*30)) # dummy time prior
    > to today
    > if time.strftime("%d", mytime) == today_day:
    > print time.strftime("%H:%M", mytime)
    > else:
    > print time.strftime("%b%d", mytime)


    Well, date/datetime objects are directly comparable:

    import datetime
    today_day = datetime.date.today()
    other = datetime.datetime.fromtimestamp(your_timestamp)
    if other.date() == today_day:
    fmt = "%H:%M"
    else:
    fmt = "%b%d"
    print other.strftime(fmt)

    -tkc
    Tim Chase, Mar 25, 2009
    #3
  4. On 25 Mar, 14:51, Tim Chase <> wrote:
    > > I have a date expressed in seconds.
    > > I'd want to pretty print it as "%H:%M" if the time refers to today and
    > > "%b%d" (month, day) if it's of yesterday or before.

    >
    > > I managed to do that with the code below but I don't like it too much.
    > > Is there a better way to do that?
    > > Thanks in advance.

    >
    > > import time

    >
    > > today_day = time.strftime("%d", time.localtime(time.time()))
    > > mytime = time.localtime(time.time() - (60*60*30))  # dummy time prior
    > > to today
    > > if time.strftime("%d", mytime) == today_day:
    > >     print time.strftime("%H:%M", mytime)
    > > else:
    > >     print time.strftime("%b%d", mytime)

    >
    > Well, date/datetime objects are directly comparable:
    >
    >    import datetime
    >    today_day = datetime.date.today()
    >    other = datetime.datetime.fromtimestamp(your_timestamp)
    >    if other.date() == today_day:
    >      fmt = "%H:%M"
    >    else:
    >      fmt = "%b%d"
    >    print other.strftime(fmt)
    >
    > -tkc


    Thanks, it did the job.

    --- Giampaolo
    http://code.google.com/p/pyftpdlib
    http://code.google.com/p/psutil
    Giampaolo Rodola', Mar 25, 2009
    #4
  5. Giampaolo Rodola'

    John Machin Guest

    On Mar 26, 12:51 am, Tim Chase <> wrote:
    > > I have a date expressed in seconds.
    > > I'd want to pretty print it as "%H:%M" if the time refers to today and
    > > "%b%d" (month, day) if it's of yesterday or before.

    >
    > > I managed to do that with the code below but I don't like it too much.
    > > Is there a better way to do that?
    > > Thanks in advance.

    >
    > > import time

    >
    > > today_day = time.strftime("%d", time.localtime(time.time()))
    > > mytime = time.localtime(time.time() - (60*60*30))  # dummy time prior
    > > to today
    > > if time.strftime("%d", mytime) == today_day:
    > >     print time.strftime("%H:%M", mytime)
    > > else:
    > >     print time.strftime("%b%d", mytime)

    >
    > Well, date/datetime objects are directly comparable:
    >
    >    import datetime
    >    today_day = datetime.date.today()
    >    other = datetime.datetime.fromtimestamp(your_timestamp)
    >    if other.date() == today_day:
    >      fmt = "%H:%M"
    >    else:
    >      fmt = "%b%d"
    >    print other.strftime(fmt)
    >
    > -tkc


    time tuples are directly comparable, too:

    import time
    today_day = time.localtime(time.time())[:3]
    other = time.localtime(your_timestamp)
    if other[:3] == today_day:
    etc
    etc

    Cheers,
    John
    John Machin, Mar 25, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TB
    Replies:
    0
    Views:
    440
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,197
    Michael Borgwardt
    May 30, 2004
  3. Jim in Arizona

    Finding a prior date by subtracting from today

    Jim in Arizona, Dec 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    393
    Mark Rae
    Dec 27, 2006
  4. Vinnie Davidson

    ASP Date: get records with date = today (SQL Server)

    Vinnie Davidson, Aug 13, 2005, in forum: ASP General
    Replies:
    6
    Views:
    310
    Aaron Bertrand [SQL Server MVP]
    Aug 15, 2005
  5. joeyej
    Replies:
    7
    Views:
    128
    Randy Webb
    Apr 6, 2006
Loading...

Share This Page