Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String number to Date format

Status
Not open for further replies.

GoSooJJ

Programmer
Feb 24, 2001
76
US
This can be simple but I'm trying to find out best way of converting string integer to date format. Maybe use regular expression for this?

there is input string which is 6, or 8 numeric characters.
if 6 characters then first 4 digit is year and last 2 digit is month.
If 8 characters then first 4 digit is year, next 2 digit is month, and last 2 digit is day.

I can use 'if' statement to get date but i have many date fields. do you have any other idea? ^_^

thank you!
 
if 6 characters then first 4 digit is year and last 2 digit is month.

By the typical definition, that is not a date. There is no day of month. So what do you mean by "date format"?

but i have many date fields

What do you mean many date fields (variables, columns in a query, ...?)

----------------------------------
 
Sorry if my original question wasn't clear. Anyways, I fixed it out. Here is an example that I just tested and works fine.

<cfoutput>
<cfset str = '200909' />
<cfset str = '20090903' />

<cfif len(str) EQ 6>
<cfset str = dateFormat(parseDateTime(reReplace(str, "([0-9]{4})([0-9]{2})$", "\1/\2", "All")), "mm/dd/yyyy") />
<cfelseif len(str) EQ 8>
<cfset str = dateFormat(parseDateTime(reReplace(str, "([0-9]{4})([0-9]{2})([0-9]{2})$", "\1/\2/\3", "All")), "mm/dd/yyyy") />
</cfif>
#str#<br />
</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top