Get System Date?

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

    #1

    Get System Date?

    Is it possible to get the system date on a Windows XP machine? Most
    Convenient would to retrieve YYYY, MM, and DD as seperate variables.

    When I say system date, I'm thinking of the small clock in the
    lower-right hand corner, which has date as well as time, but if there's
    another clock that Python supports, it works for me as long as it's
    somewhat accurate.

  • Rene Pijlman

    #2
    Re: Get System Date?

    Dustan:[color=blue]
    >Is it possible to get the system date on a Windows XP machine?[/color]

    Most certainly:
    The official home of the Python Programming Language


    --
    René Pijlman

    Comment

    • Dave

      #3
      Re: Get System Date?

      Dustan,

      Python has a module called, appropriately, "time". Like most things in
      Python, it's fairly simple and straightforward to use.

      The module is documented here:
      This module provides various time-related functions. For related functionality, see also the datetime and calendar modules. Although this module is always available, not all functions are available...


      Breifly, though, the format you want is built right into time:
      [color=blue][color=green][color=darkred]
      >>> import time
      >>> now = time.localtime( )
      >>> print now[/color][/color][/color]
      (2006, 2, 5, 13, 21, 15, 6, 36, 0)

      So to get the year, month, and day you would just have to manipulate
      the values returned by time.gmtime().

      - Dave

      Comment

      • Dustan

        #4
        Re: Get System Date?

        Thanks

        Comment

        • Alex Martelli

          #5
          Re: Get System Date?

          Dustan <DustanGroups@g mail.com> wrote:
          [color=blue]
          > Is it possible to get the system date on a Windows XP machine? Most
          > Convenient would to retrieve YYYY, MM, and DD as seperate variables.
          >
          > When I say system date, I'm thinking of the small clock in the
          > lower-right hand corner, which has date as well as time, but if there's
          > another clock that Python supports, it works for me as long as it's
          > somewhat accurate.[/color]
          [color=blue][color=green][color=darkred]
          >>> import datetime
          >>> x=datetime.date .today()
          >>> yyyy, mm, dd = x.year, x.month, x.day
          >>> print yyyy[/color][/color][/color]
          2006[color=blue][color=green][color=darkred]
          >>> print mm[/color][/color][/color]
          2[color=blue][color=green][color=darkred]
          >>> print dd[/color][/color][/color]
          5[color=blue][color=green][color=darkred]
          >>>[/color][/color][/color]

          As you see, these are integers (so, for example, mm is not '02' but just
          2). Pretty easy to make them into strings, of course, if you want.


          Alex

          Comment

          • Magnus Lycka

            #6
            Re: Get System Date?

            Dave wrote:[color=blue]
            > Dustan,
            >
            > Python has a module called, appropriately, "time".[/color]

            This is basically a wrapper around the standard C time
            library. Python has a more modern and spiffy datetime
            module which isn't restrained to 1970-2038, and just
            handles spiffy date and datetime objects instead of
            making you deal with awkward conversions between strange
            numbers and obscure tuples. The old time module does work,
            but due to e.g. strange assymetries in the module, you
            have to deal with strange quirks to convert UTC times
            right...
            [color=blue][color=green][color=darkred]
            >>> import datetime
            >>> datetime.dateti me.now()[/color][/color][/color]
            datetime.dateti me(2006, 2, 5, 23, 1, 45, 569833)

            Comment

            Working...