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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a function to convert a date to a number

Status
Not open for further replies.

Stoemp

Programmer
Sep 25, 2002
389
0
0
BE
Hi,

I like to use an alternative date notation. An example here:

Date: 27 october 2003 -> 20031027

This date notation is a lot easier to handle because it's a number. I wrote a funcion to convert a normal date to this notation, but I can't call this function. The function is:

Code:
Sub converteerDatumNumerisch(date)
    
    dNumTemp = Year(date)
    
    If Len(Month(date)) = 1 Then
        
        dNumTemp = dNumTemp & "0" & Month(date)
        
    Else
    
        dNumTemp = dNumTemp & Month(date)
    
    End If
    
    If Len(Day(date)) = 1 Then
        
        dNumTemp = dNumTemp & "0" & Day(date)
        
    Else
    
        dNumTemp = dNumTemp & Day(date)
    
    End If

    Application("convertedDate") = dNumTemp
    
End Sub

This function is declared in my global.asa. I call this function in my Session_OnStart with the following line:
Code:
converteerDatumNumerisch(Date())

I get an error: Types do not match

Does anyone have a clue what I'm doing wrong? I'm pretty new in this whole date managing stuff and it's not really simple.

Thanks,
Steven
 
The return of Year(date), Month(date) and day(date) are most probably numeric. But Len function needs a character string for evaluation. This may be the type mismatch. Try the following:

Code:
If Month(date) = 1 Then
        dNumTemp = dNumTemp & "0" & Month(date)
    Else
        dNumTemp = dNumTemp & Month(date)
    End If
    
    If Day(date) = 1 Then
        dNumTemp = dNumTemp & "0" & Day(date)
    Else
        dNumTemp = dNumTemp & Day(date)
    End If

Shihab
 
The weird thing is that the function already worked but I don't know how. I was already able to retreive a correct number from this function so the function has to be correct. Normally when this type mismatch you suggest would be an issue, then I will get the debugger message on the exact line in the function and not in the function call I guess. Thanks anyway for the info.

Steven
 
Hello Stoemp,

Should you not call it like this?
Code:
    converteerDatumNumerisch Date()
regards - tsuji
 
Addenda

Also, would it not be better to make it a function, and keep the line Application(" ") within the Session_onStart?
Furthermore, avoid using "date" as param name appeared as the parameter passing to converteer...()?
Code:
Function converteerDatumNumerisch(dtDate)  'Avoid calling it date?
    ...etc
    'Application("convertedDate") = dNumTemp  'commented out
    converteerDatunNumerisch = dNumTemp
End Function
And in your Session_onStart, do something like this.
Code:
Sub Session_onStart
   '...etc...
   Application("convertedDate") = converteerDatumNumerisch(date())
   '...etc
End Sub
- tsuji
 
Just to shorten it a bit for you:

Code:
Function converteerDatumNumerisch(dtDate)  
    converteerDatumNumerisch = Year(dtDate) & Right("0" & Month(dtDate), 2) & Right("0" & Day(dtDate), 2)
End Function

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks for the help, but I guess I'll just use the normal date stuff. I had too much trouble and just doing the effort to understand normal date manipulation will be the right thing to do I think.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top