date/time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nader Emami

    #1

    date/time

    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
  • Binu K S

    #2
    Re: date/time

    >>> import time[color=blue][color=green][color=darkred]
    >>> time.strftime(' %Y%m%d',time.lo caltime())[/color][/color][/color]
    '20050105'

    On Wed, 05 Jan 2005 15:08:37 +0100, Nader Emami <emami@knmi.n l> wrote:[color=blue]
    > 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
    >[/color]

    Comment

    • Thomas Guettler

      #3
      Re: date/time

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

      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(timetupl e) # --> secs
      - strftime(localt ime(secs))

      HTH,
      Thomas

      --
      Thomas Güttler, http://www.thomas-guettler.de/


      Comment

      • Lee Harr

        #4
        Re: date/time

        On 2005-01-05, Nader Emami <emami@knmi.n l> wrote:[color=blue]
        > 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.
        >[/color]


        I would use the datetime module:

        [color=blue][color=green][color=darkred]
        >>> import datetime
        >>> today = datetime.date.t oday()
        >>> today[/color][/color][/color]
        datetime.date(2 005, 1, 5)[color=blue][color=green][color=darkred]
        >>> today.strftime( '%Y%m%d')[/color][/color][/color]
        '20050105'[color=blue][color=green][color=darkred]
        >>> one_day = datetime.timede lta(1)
        >>> tomorrow = today + one_day
        >>> tomorrow.strfti me('%Y%m%d')[/color][/color][/color]
        '20050106'


        Comment

        • David M. Cooke

          #5
          Re: date/time

          Thomas Guettler <guettli@thom as-guettler.de> writes:
          [color=blue]
          > Am Wed, 05 Jan 2005 15:08:37 +0100 schrieb Nader Emami:
          >[color=green]
          >> L.S.,
          >>
          >> Could somebody help me how I can get the next format of date
          >> from the time module?[/color]
          >
          > 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(timetupl e) # --> secs
          > - strftime(localt ime(secs))[/color]

          Or using the datetime module:

          import time, datetime

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


          --
          |>|\/|<
          /--------------------------------------------------------------------------\
          |David M. Cooke
          |cookedm(at)phy sics(dot)mcmast er(dot)ca

          Comment

          Working...