posted on Sunday, April 29, 2007 3:27 AM by Obi

How to Format Dates in .Net

 '-------------------------------------------------------------------------------------
' Determines the date of the Sunday of the week for the incoming date
'-------------------------------------------------------------------------------------
Function GetSundayDate(strDateIn)

   dim intDayOfWeek

   intDayOfWeek = weekday(strDateIn, 1)
   intDayOfWeek = (intDayOfWeek * (-1)) + 1

   GetSundayDate = formatdate(dateadd("d",strDateIn ,intDayOfWeek))
  
End Function


'-------------------------------------------------------------------------------------
' Formats a date in mm/dd/yyyy format
'-------------------------------------------------------------------------------------
Function FormatDate(strDateIn)

   FormatDate = datepart("m", strDateIn) & "/" & datepart("d", strDateIn) & "/" & datepart("yyyy", strDateIn)
  
End Function


'-------------------------------------------------------------------------------------
' Returns a formatted "hh:mm AM/PM" string for the incoming 24-hour formatted time
'-------------------------------------------------------------------------------------
Function FormatTime(intTime)

   dim strTime

   if intTime < 1000 then
      strTime = cstr(intTime)
      FormatTime = left(strTime,1) & ":" & right(strTime,2) & " AM"
   elseif intTime < 1200 then
      strTime = cstr(intTime)
      FormatTime = left(strTime,2) & ":" & right(strTime,2) & " AM"
   elseif intTime < 1300 then
      strTime = cstr(intTime)
      FormatTime = left(strTime,2) & ":" & right(strTime,2) & " PM"
   else
      strTime = cstr(intTime - 1200)
      FormatTime = left(strTime,1) & ":" & right(strTime,2) & " PM"
   end if

End Function


'-------------------------------------------------------------------------------------
' Returns a mm/dd/yy formatted date for the incoming Sunday date and the requested day of the week
'-------------------------------------------------------------------------------------
Function GetDate(strDateIn, intDayOfWeek)

   GetDate = formatdate(dateadd("d", intDayOfWeek, strDateIn))

End Function

Happy Coding,

Obi

 

Comments