date/time

Discussion in 'Python' started by Nader Emami, Jan 5, 2005.

  1. Nader Emami

    Nader Emami Guest

    L.S.,

    Could somebody help me how I can get the next format of date
    from the time module?

    example: I have to have this time 20050105. It is the next
    attributes of format %Y%m%d.

    with regards,
    Nader
    Nader Emami, Jan 5, 2005
    #1
    1. Advertising

  2. Nader Emami

    Binu K S Guest

    >>> import time
    >>> time.strftime('%Y%m%d',time.localtime())

    '20050105'

    On Wed, 05 Jan 2005 15:08:37 +0100, Nader Emami <> wrote:
    > L.S.,
    >
    > Could somebody help me how I can get the next format of date
    > from the time module?
    >
    > example: I have to have this time 20050105. It is the next
    > attributes of format %Y%m%d.
    >
    > with regards,
    > Nader
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Binu K S, Jan 5, 2005
    #2
    1. Advertising

  3. Am Wed, 05 Jan 2005 15:08:37 +0100 schrieb Nader Emami:

    > L.S.,
    >
    > Could somebody help me how I can get the next format of date
    > from the time module?


    I don't understand your question. Do you want to have the next day?

    20041231 --> 20050101 ?

    You can do it like this:
    - parse the string with time.strptime
    - timetuple[2]+=1
    - mktime(timetuple) # --> secs
    - strftime(localtime(secs))

    HTH,
    Thomas

    --
    Thomas G├╝ttler, http://www.thomas-guettler.de/
    Thomas Guettler, Jan 5, 2005
    #3
  4. Nader Emami

    Lee Harr Guest

    On 2005-01-05, Nader Emami <> wrote:
    > L.S.,
    >
    > Could somebody help me how I can get the next format of date
    > from the time module?
    >
    > example: I have to have this time 20050105. It is the next
    > attributes of format %Y%m%d.
    >



    I would use the datetime module:


    >>> import datetime
    >>> today = datetime.date.today()
    >>> today

    datetime.date(2005, 1, 5)
    >>> today.strftime('%Y%m%d')

    '20050105'
    >>> one_day = datetime.timedelta(1)
    >>> tomorrow = today + one_day
    >>> tomorrow.strftime('%Y%m%d')

    '20050106'
    Lee Harr, Jan 5, 2005
    #4
  5. Thomas Guettler <> writes:

    > Am Wed, 05 Jan 2005 15:08:37 +0100 schrieb Nader Emami:
    >
    >> L.S.,
    >>
    >> Could somebody help me how I can get the next format of date
    >> from the time module?

    >
    > I don't understand your question. Do you want to have the next day?
    >
    > 20041231 --> 20050101 ?
    >
    > You can do it like this:
    > - parse the string with time.strptime
    > - timetuple[2]+=1
    > - mktime(timetuple) # --> secs
    > - strftime(localtime(secs))


    Or using the datetime module:

    import time, datetime

    tt = time.strptime('20041231', '%Y%m%d')
    t = datetime.date.fromtimestamp(time.mktime(tt))
    # now in a easier-to-handle form than the time tuple
    t += datetime.timedelta(days=1)
    print t.strftime('%Y%m%d')


    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
    David M. Cooke, Jan 5, 2005
    #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. Chris Berg
    Replies:
    0
    Views:
    772
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,193
    Michael Borgwardt
    May 30, 2004
  3. Keith Cochrane
    Replies:
    2
    Views:
    597
    Keith Cochrane
    Aug 6, 2006
  4. flamesrock
    Replies:
    8
    Views:
    433
    Hendrik van Rooyen
    Nov 24, 2006
  5. Summu82
    Replies:
    5
    Views:
    847
    Richard Heathfield
    Jun 7, 2006
Loading...

Share This Page