Wednesday, November 15, 2006

Visual Basic: Format Date And Time

Useful function for VB programmer to format date in standard strings

function ReturnFormattedTime (mytime)
Dim CurrentTime, nHour, nMinute, nSecond
Dim strHour, strMinute, strSecond

CurrentTime = mytime
nHour = Hour( CurrentTime )
nMinute = Minute( CurrentTime )
nSecond = Second( CurrentTime)


strHour = CStr( nHour )
if nHour < 10 then strHour = "0" & strHour

strMinute = CStr( nMinute )
if nMinute < 10 then strMinute = "0" & strMinute

strSecond = CStr( nSecond )
if nSecond < 10 then strSecond = "0" & strSecond

ReturnFormattedTime = strHour & ":" & strMinute & ":" & strSecond

end function



function ReturnFormattedDate (mydate)
Dim CurrentDate, nYear, nMonth, nDay
Dim strYear, strMonth, strDay


CurrentDate = mydate
nYear = Year( CurrentDate )
nMonth = Month( CurrentDate )
nDay = Day( CurrentDate )

strYear = CStr( nYear )

strMonth = CStr( nMonth )
if nMonth < 10 then strMonth = "0" & strMonth

strDay = CStr( nDay )
if nDay < 10 then strDay = "0" & strDay

ReturnFormattedDate = strDay & "/" & strMonth & "/" & strYear

end function


Tag:, , , , ,

0 Comments:

Post a Comment

<< Home