VBScript Date Format Functions


/ Published in: ASP
Save to your folder(s)

Format strings:

* %A - AM or PM
* %a - am or pm
* %m - Month with leading zeroes (01 - 12)
* %n - Month without leading zeroes (1 - 12)
* %F - Month name (January - December)
* %M - Three letter month name (Jan - Dec)
* $d - Day with leading zeroes (01 - 31)
* %j - Day without leading zeroes (1 - 31)
* %H - Hour with leading zeroes (12 hour)
* %h - Hour with leading zeroes (24 hour)
* %G - Hour without leading zeroes (12 hour)
* %g - Hour without leading zeroes (24 hour)
* %i - Minute with leading zeroes (01 to 60)
* %I - Minute without leading zeroes (1 to 60)
* %s - Second with leading zeroes (01 to 60)
* %S - Second without leading zeroes (1 to 60)
* %L - Number of day of week (1 to 7)
* %l - Name of day of week (Sunday to Saturday)
* %D - Three letter name of day of week (Sun to Sat)
* %O - Ordinal suffix (st, nd rd, th)
* %U - UNIX Timestamp
* %Y - Four digit year (2003)
* %y - Two digit year (03)


Copy this code and paste it in your HTML
  1. function formatDate(format, intTimeStamp)
  2. dim unUDate, A
  3.  
  4. ' Test to see if intTimeStamp looks valid. If not, they have passed a normal date
  5. if not (isnumeric(intTimeStamp)) then
  6. if isdate(intTimeStamp) then
  7. intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
  8. else
  9. response.write "Date Invalid"
  10. exit function
  11. end if
  12. end if
  13.  
  14. if (intTimeStamp=0) then
  15. unUDate = now()
  16. else
  17. unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
  18. end if
  19.  
  20. unUDate = trim(unUDate)
  21.  
  22. dim startM : startM = InStr(1, unUDate, "/", vbTextCompare) + 1
  23. dim startY : startY = InStr(startM, unUDate, "/", vbTextCompare) + 1
  24. dim startHour : startHour = InStr(startY, unUDate, " ", vbTextCompare) + 1
  25. dim startMin : startMin = InStr(startHour, unUDate, ":", vbTextCompare) + 1
  26.  
  27. dim dateDay : dateDay = mid(unUDate, 1, 2)
  28. dim dateMonth : dateMonth = mid(unUDate, startM, 2)
  29. dim dateYear : dateYear = mid(unUDate, startY, 4)
  30. dim dateHour : dateHour = mid(unUDate, startHour, 2)
  31. dim dateMinute : dateMinute = mid(unUDate, startMin, 2)
  32. dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":", vbTextCompare) + 1, 2)
  33.  
  34. format = replace(format, "%Y", right(dateYear, 4))
  35. format = replace(format, "%y", right(dateYear, 2))
  36. format = replace(format, "%m", dateMonth)
  37. format = replace(format, "%n", cint(dateMonth))
  38. format = replace(format, "%F", monthname(cint(dateMonth)))
  39. format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
  40. format = replace(format, "%d", dateDay)
  41. format = replace(format, "%j", cint(dateDay))
  42. format = replace(format, "%h", mid(unUDate, startHour, 2))
  43. format = replace(format, "%g", cint(mid(unUDate, startHour, 2)))
  44.  
  45. if (cint(dateHour) > 12) then
  46. A = "PM"
  47. else
  48. A = "AM"
  49. end if
  50. format = replace(format, "%A", A)
  51. format = replace(format, "%a", lcase(A))
  52.  
  53. if (A = "PM") then format = replace(format, "%H", left("0" & dateHour - 12, 2))
  54. format = replace(format, "%H", dateHour)
  55. if (A = "PM") then format = replace(format, "%G", left("0" & cint(dateHour) - 12, 2))
  56. format = replace(format, "%G", cint(dateHour))
  57.  
  58. format = replace(format, "%i", dateMinute)
  59. format = replace(format, "%I", cint(dateMinute))
  60. format = replace(format, "%s", dateSecond)
  61. format = replace(format, "%S", cint(dateSecond))
  62. format = replace(format, "%L", WeekDay(unUDate))
  63. format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3))
  64. format = replace(format, "%l", WeekDayName(WeekDay(unUDate)))
  65. format = replace(format, "%U", intTimeStamp)
  66. format = replace(format, "11%O", "11th")
  67. format = replace(format, "1%O", "1st")
  68. format = replace(format, "12%O", "12th")
  69. format = replace(format, "2%O", "2nd")
  70. format = replace(format, "13%O", "13th")
  71. format = replace(format, "3%O", "3rd")
  72. format = replace(format, "%O", "th")
  73.  
  74. formatDate = format
  75.  
  76. end function

URL: http://www.addedbytes.com/asp/vbscript-date-format-functions/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.