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!

Converting a Numeric field into a Date field

Status
Not open for further replies.

odessa79

Programmer
Sep 16, 2003
31
US
Hi,
I have a field coming back from the database which is Numeric 8 position (19930203) I want to convert it into 02/03/1993 how would I do that?
 

By using the following functions...
CStr
Left
Mid
Right
CDate
To create your own function if you wish to convert your double to a date...

Good Luck

 
hi,

Code:
Option Explicit
Private Sub Command1_Click()
    Dim yourDate As Double
    Dim myDate As Date
    Dim myString As String
    
    yourDate = 19930203
    myString = Trim(Str(yourDate))
    
    'if your last 2 char are the day
    myString = Right(Left(myString, 6), 2) & "/" & _
                Right(myString, 2) & "/" & Left(myString, 4)
        
    Debug.Print (myString)
    
    myDate = Format(myString, "mm/dd/yyyy")
    myString = Format(myDate, "mm/dd/yy")
    
    Debug.Print (myString)
End Sub

good luck,

--
Luis MX
 

All of the functions that I mention are well documented in the help files. At this point I will not "GIVE" you a solution but I will help you create your own solution.

So your original input into this function would be a numeric value ... (19930203) ... (yyyymmdd)

And you can optionally convert it to a string (CStr) to use the string manipulation functions that follow ...

If you look up Left you will find it fairly well documented and self explanatory. The Left function is used to retrieve the yyyy part of the double date.

Then if you look up mid you will find it well documented also. The Mid function is used to retrieve the mm from the double date. So that would be starting at postion 5 for a length of 2.

Then if you look up the right function you will see that it is very similar to the left function ... except it start from the right side and is used to retrieve the dd part of the double date.

Now with these 3 (4) functions you have optionally converted the double to a string and have the ability to parse off each segment of the date that you need (year, month, and day).

Then with some string concatenation you would take the values MM & "/" & DD & "/" & YYYY that you have retrieved and optionally wrap them in the CDate function to return a date from the function (or string if you wish (or varient)).

And as a side note you may also want to look up the format function.

Good Luck

 
Odessa,

vb5prgrmr's post might seem a bit off, but trust me, this guy is doing you one hell of a favour. The best way to learn any language, especially the fundamentals, is through playing with it.

MSDN's documentation is not bad, and should be enough to get you started...

mmilan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top