convert DATE to STRING format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • catgovind
    New Member
    • Feb 2007
    • 1

    convert DATE to STRING format

    Hello

    I have a date format '01/01/1000' I want to convert that date to string "01012000". Please help me how do I do
    that?
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    Originally posted by catgovind
    Hello

    I have a date format '01/01/1000' I want to convert that date to string "01012000". Please help me how do I do
    that?
    If it is a typo and you don't really want to change 1000 to 2000 then
    Code:
    DateString='01/01/1000'
    DateString.replace('/','')
    '01011000'
    would do it. If you really want to add something to the year, then splitting the string and putting it back together would be necessary.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by catgovind
      Hello

      I have a date format '01/01/1000' I want to convert that date to string "01012000". Please help me how do I do
      that?
      Code:
      >>> s = '01/01/1000'
      >>> s = s.replace('/', '')
      >>> s
      '01011000'
      Code:
      >>> s = '01/01/1000'
      >>> ''.join(s.split('/'))
      '01011000'
      >>>
      Did you just want to remove the '/'?

      Comment

      • dshimer
        Recognized Expert New Member
        • Dec 2006
        • 136

        #4
        If you need to add something (like 1000) then the following based on the DateString created in the previous post.
        Code:
        repr(string.atoi(DateString.split('/')[2])+1000)
        Would
        Split the date string at the slashes........ ..DateString.spli t('/')
        return the 3rd member which is the 1000..........[2]
        change the string to an int..........string.atoi()
        Add 1000 to it (the int)..........+1000
        change it back to a string......... .repr()
        Which could then be joined back to the other elements.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by dshimer
          If you need to add something (like 1000) then the following based on the DateString created in the previous post.
          Code:
          repr(string.atoi(DateString.split('/')[2])+1000)
          Would
          Split the date string at the slashes........ ..DateString.spli t('/')
          return the 3rd member which is the 1000..........[2]
          change the string to an int..........string.atoi()
          Add 1000 to it (the int)..........+1000
          change it back to a string......... .repr()
          Which could then be joined back to the other elements.
          dshimer,

          From Python 2.3 documentation:
          'atoi( s[, base])

          Deprecated since release 2.0. Use the int() built-in function.'

          I have been severely scolded on this forum in the past for using the string module!

          Comment

          • dshimer
            Recognized Expert New Member
            • Dec 2006
            • 136

            #6
            Originally posted by bvdet
            dshimer,

            From Python 2.3 documentation:
            'atoi( s[, base])

            Deprecated since release 2.0. Use the int() built-in function.'

            I have been severely scolded on this forum in the past for using the string module!
            Thanks for keeping an eye on me, the vast majority of my code is written for an embedded version to add function to my CAD program. They just switched from 2.2 to 2.5 and my tendency is to just go back to things I know or the first thing that comes to mind, which are rarely the best way. Should have caught that one though.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by dshimer
              Thanks for keeping an eye on me, the vast majority of my code is written for an embedded version to add function to my CAD program. They just switched from 2.2 to 2.5 and my tendency is to just go back to things I know or the first thing that comes to mind, which are rarely the best way. Should have caught that one though.
              No problem. What type of CAD? A Python interpreter is embedded in it? All my Python production work is executed by an embedded interpreter in SDS/2 which is 3D CAD for structural steel detailing. We are still on 2.3. Generally what type of operations do your scripts perform?

              Comment

              • dshimer
                Recognized Expert New Member
                • Dec 2006
                • 136

                #8
                Originally posted by bvdet
                No problem. What type of CAD? A Python interpreter is embedded in it? All my Python production work is executed by an embedded interpreter in SDS/2 which is 3D CAD for structural steel detailing. We are still on 2.3. Generally what type of operations do your scripts perform?
                It's called VrOne and is primarily for topographic mapping and collecting raw data for Geographic Information Systems. We have full access to the data for batch processing and analysis, along with a host of individual entity (lines, symbols, text) identification, attribute extraction, modification functions. Lots of helpful GUI stuff thrown in so it is possible to write from scratch any function that you think the software should have. Mine tend to run the whole range from simple time saver convenience functions, to batch data processing and analysis. Python is one of the things that makes the job really fun.
                Though now I think of it I'm getting really off topic.

                Comment

                Working...