Connecting Tech Pros Worldwide Help | Site Map

convert DATE to STRING format

Newbie
 
Join Date: Feb 2007
Posts: 1
#1: Feb 7 '07
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's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#2: Feb 7 '07

re: convert DATE to STRING format


Quote:

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
Expand|Select|Wrap|Line Numbers
  1. DateString='01/01/1000'
  2. DateString.replace('/','')
  3. '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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#3: Feb 7 '07

re: convert DATE to STRING format


Quote:

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?

Expand|Select|Wrap|Line Numbers
  1. >>> s = '01/01/1000'
  2. >>> s = s.replace('/', '')
  3. >>> s
  4. '01011000'
Expand|Select|Wrap|Line Numbers
  1. >>> s = '01/01/1000'
  2. >>> ''.join(s.split('/'))
  3. '01011000'
  4. >>> 
Did you just want to remove the '/'?
dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#4: Feb 7 '07

re: convert DATE to STRING format


If you need to add something (like 1000) then the following based on the DateString created in the previous post.
Expand|Select|Wrap|Line Numbers
  1. repr(string.atoi(DateString.split('/')[2])+1000)
Would
Split the date string at the slashes..........DateString.split('/')
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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#5: Feb 7 '07

re: convert DATE to STRING format


Quote:

Originally Posted by dshimer

If you need to add something (like 1000) then the following based on the DateString created in the previous post.

Expand|Select|Wrap|Line Numbers
  1. repr(string.atoi(DateString.split('/')[2])+1000)
Would
Split the date string at the slashes..........DateString.split('/')
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!
dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#6: Feb 7 '07

re: convert DATE to STRING format


Quote:

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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#7: Feb 7 '07

re: convert DATE to STRING format


Quote:

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?
dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#8: Feb 7 '07

re: convert DATE to STRING format


Quote:

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.
Reply