Emulate date-related portion of VB's Format function

Author Message
jpaxton

  • Total Posts : 4
  • Scores: 0
  • Reward points : 0
  • Joined: 9/14/2006
  • Status: offline
Emulate date-related portion of VB's Format function Friday, September 15, 2006 4:12 PM (permalink)
0
Here's an attempt to emulate the date-related portion of Visual Basic's Format function.  I did not need the time-related functionality, but it could be added easily
 
  
 Public Function FormatDate(ByVal sDate, ByVal sFormat)
  
    If Not IsDate(sDate) Then 
       FormatDate = "Invalid date"
       Exit Function
    End If
  
    'Set the possible date-related values
    d = Day(sDate)
    dd = Right("0" & Day(sDate), 2)
    DDD = WeekdayName(Weekday(sDate), True)
    DDDD = WeekdayName(Weekday(sDate))
    m = Month(sDate)
    mm = Right("0" & Month(sDate), 2)
    MMM = MonthName(Month(sDate), True)
    MMMM = MonthName(Month(sDate))
    cc = Left(Year(sDate), 2)  'century
    yy = Right(Year(sDate), 2)
    yyyy = Year(sDate)
  
    sTempDate = sFormat
  
    'Replace formatting with actual values
    'Used case sensative search & replace due to long names
    If InStr(1, sTempDate, "yyyy", 0) > 0 Then sTempDate = Replace(sTempDate, "yyyy", yyyy)
    If InStr(1, sTempDate, "yy", 0) > 0 Then sTempDate = Replace(sTempDate, "yy", yy)
    If InStr(1, sTempDate, "dd", 0) > 0 Then sTempDate = Replace(sTempDate, "dd", dd)
    If InStr(1, sTempDate, "d", 0) > 0 Then sTempDate = Replace(sTempDate, "d", d)
    If InStr(1, sTempDate, "mm", 0) > 0 Then sTempDate = Replace(sTempDate, "mm", mm)
    If InStr(1, sTempDate, "m", 0) > 0 Then sTempDate = Replace(sTempDate, "m", m)
  
    'The long names must come last because the search & replace above will
    'incorrectly replace the values in the names
    If InStr(1, sTempDate, "DDDD", 0) > 0 Then sTempDate = Replace(sTempDate, "DDDD", DDDD)
    If InStr(1, sTempDate, "DDD", 0) > 0 Then sTempDate = Replace(sTempDate, "DDD", DDD)
    If InStr(1, sTempDate, "MMMM", 0) > 0 Then sTempDate = Replace(sTempDate, "MMMM", MMMM)
    If InStr(1, sTempDate, "MMM", 0) > 0 Then sTempDate = Replace(sTempDate, "MMM", MMM)
  
    FormatDate = sTempDate
  
 End Function
  
 

 
#1

    Online Bookmarks Sharing: Share/Bookmark

    Jump to:

    Current active users

    There are 0 members and 1 guests.

    Icon Legend and Permission

    • New Messages
    • No New Messages
    • Hot Topic w/ New Messages
    • Hot Topic w/o New Messages
    • Locked w/ New Messages
    • Locked w/o New Messages
    • Read Message
    • Post New Thread
    • Reply to message
    • Post New Poll
    • Submit Vote
    • Post reward post
    • Delete my own posts
    • Delete my own threads
    • Rate post

    2000-2012 ASPPlayground.NET Forum Version 3.9