51Testing软件测试论坛

标题: 提供一个处理日期的函数-VBScript的 [打印本页]

作者: javaleo    时间: 2007-5-21 21:57
标题: 提供一个处理日期的函数-VBScript的
QTP是用VBScript的,而VBScript里面关于日期格式的过程很少,这里有一个过程,可以得到各种日期格式

  1. function formatDate(format, intTimeStamp)
  2. Dim monthname()
  3. Redim monthname(12)
  4. monthname(1) = "January"
  5. monthname(2) = "February"
  6. monthname(3) = "March"
  7. monthname(4) = "April"
  8. monthname(5) = "May"
  9. monthname(6) = "June"
  10. monthname(7) = "July"
  11. monthname(8) = "August"
  12. monthname(9) = "September"
  13. monthname(10) = "October"
  14. monthname(11) = "November"
  15. monthname(12) = "December"

  16. dim unUDate, A

  17. dim OriginalLocale
  18. dim res
  19. OriginalLocale = GetLocale
  20. res = SetLocale("en-gb")

  21. ' Test to see if intTimeStamp looks valid. If not, they have passed a normal date
  22. if not (isnumeric(intTimeStamp)) then
  23. if isdate(intTimeStamp) then
  24. intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
  25. else
  26. response.write "Date Invalid"
  27. exit function
  28. end if
  29. end if

  30. if (intTimeStamp=0) then
  31. unUDate = now()
  32. else
  33. unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
  34. end if

  35. unUDate = trim(unUDate)

  36. 'bug fix for midnight problems
  37. If (Len(unUDate) <= 11) Then unUDate = Trim(unUDate) & " 00:00:00"

  38. dim startM : startM = 1
  39. dim startD : startD = InStr(startM, unUDate, "/")+1
  40. dim startY : startY = InStr(startD, unUDate, "/")+1
  41. dim startHour : startHour = InStr(startY, unUDate, " ")+1
  42. dim startMin : startMin = InStr(startHour, unUDate, ":")+1
  43. dim startSec : startSec = InStr(startMin+1, unUDate, ":")+1
  44. dim dateMonth : dateMonth = mid(unUDate, startD, ((startY - 1) - startD))
  45. dim dateDay : dateDay = mid(unUDate, 1, ((startD - 1) - 1))
  46. dim dateYear : dateYear = Year(unUDate)
  47. dim dateHour : dateHour = mid(unUDate, startHour, ((startMin - startHour) - 1))
  48. dim dateMinute : dateMinute = mid(unUDate, startMin, 2)
  49. dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":") + 1, 2)

  50. format = replace(format, "%Y", right(dateYear, 4))
  51. format = replace(format, "%y", right(dateYear, 2))
  52. format = replace(format, "%m", dateMonth)
  53. format = replace(format, "%n", cint(dateMonth))

  54. ' Response.Write CStr(cint(dateMonth))
  55. ' Response.Flush

  56. format = replace(format, "%F", monthname(cint(dateMonth)))
  57. format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
  58. format = replace(format, "%d", dateDay)
  59. format = replace(format, "%j", cint(dateDay))
  60. format = replace(format, "%h", mid(unUDate, startHour, 2))
  61. format = replace(format, "%g", cint(mid(unUDate, startHour, 2)))

  62. if (cint(dateHour) > 12) then
  63. A = "PM"
  64. else
  65. A = "AM"
  66. end if
  67. format = replace(format, "%A", A)
  68. format = replace(format, "%a", lcase(A))

  69. if (A = "PM") then format = replace(format, "%H", Right("00" & dateHour - 12, 2))
  70. format = replace(format, "%H", dateHour)
  71. if (A = "PM") then format = replace(format, "%G", left("0" & cint(dateHour) - 12, 2))
  72. format = replace(format, "%G", cint(dateHour))

  73. format = replace(format, "%i", dateMinute)
  74. format = replace(format, "%I", cint(dateMinute))
  75. format = replace(format, "%s", dateSecond)
  76. format = replace(format, "%S", cint(dateSecond))
  77. format = replace(format, "%L", WeekDay(unUDate))
  78. format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3))
  79. format = replace(format, "%l", WeekDayName(WeekDay(unUDate)))
  80. format = replace(format, "%U", intTimeStamp)
  81. format = replace(format, "11%O", "11th")
  82. format = replace(format, "1%O", "1st")
  83. format = replace(format, "12%O", "12th")
  84. format = replace(format, "2%O", "2nd")
  85. format = replace(format, "13%O", "13th")
  86. format = replace(format, "3%O", "3rd")
  87. format = replace(format, "%O", "th")

  88. formatDate = format
  89. res = SetLocale(OriginalLocale)

  90. end function
复制代码



用法

  1. eg: strDateTime = formatDate("%g:%i%a, %l %j%O %F, %Y", UDate(Now()))

  2. %A - AM or PM

  3. %a - am or pm

  4. %m - Month with leading zeroes (01 - 12)

  5. %n - Month without leading zeroes (1 - 12)

  6. %F - Month name (January - December)

  7. %M - Three letter month name (Jan - Dec)

  8. $d - Day with leading zeroes (01 - 31)

  9. %j - Day without leading zeroes (1 - 31)

  10. %H - Hour with leading zeroes (12 hour)

  11. %h - Hour with leading zeroes (24 hour)

  12. %G - Hour without leading zeroes (12 hour)

  13. %g - Hour without leading zeroes (24 hour)

  14. %i - Minute with leading zeroes (01 to 60)

  15. %I - Minute without leading zeroes (1 to 60)

  16. %s - Second with leading zeroes (01 to 60)

  17. %S - Second without leading zeroes (1 to 60)

  18. %L - Number of day of week (1 to 7)

  19. %l - Name of day of week (Sunday to Saturday)

  20. %D - Three letter name of day of week (Sun to Sat)

  21. %O - Ordinal suffix (st, nd rd, th)

  22. %U - UNIX Timestamp

  23. %Y - Four digit year (2003)

  24. %y - Two digit year (03)
复制代码

作者: walker1020    时间: 2007-5-22 09:13
谢谢楼主的无私奉献!这下对日期处理 轻松多了
作者: yuandjing    时间: 2007-5-22 16:52
好东西啊,怎么没人顶
作者: brianq    时间: 2007-5-25 11:00
谢谢
作者: fengle    时间: 2007-5-25 13:46
好东西,顶了!!
作者: walker1020    时间: 2007-6-11 09:08
不错,值得学习,偶就把它放在 [QTP精华区] 了。
作者: kxllr    时间: 2007-7-29 17:23
还不会用,不过看上去很有价值,先保存了,我一定学会用,呵呵sdlkfj2
作者: lantianwei    时间: 2007-8-15 15:01
非常感谢LZ的无私奉献!
作者: gggwavj    时间: 2007-11-11 21:59
感谢楼主无私奉献
作者: lilysun0411    时间: 2008-2-25 16:19
先下载了,学习中,3Q!
作者: span    时间: 2008-2-26 22:09
感谢楼主无私奉献
作者: language_fw    时间: 2008-2-28 16:18
标题: 回复 8# 的帖子
不错,支持lz,以后继续努力。。。
作者: machao514    时间: 2008-3-31 13:42
lz写的非常全面,常用的日期格式都有;
收藏了,谢谢搂主的无私奉献
作者: Mix    时间: 2008-3-31 16:09
收藏了,多谢楼主分享!
作者: fangfangcuky    时间: 2008-7-6 13:46
完全不懂啊
作者: wslf    时间: 2008-9-25 15:23
恩,看不太懂,要是有些注释就更好了哦
作者: zhengyh1980    时间: 2008-10-26 11:54
先看先  3ks
作者: helius    时间: 2008-10-31 09:51
太高深了
  完全看不懂

运行下 提示类型不匹配:‘Udate’
作者: lzl88    时间: 2008-11-29 17:27
标题: Thank you !

作者: Linda0721    时间: 2008-12-1 12:46
收藏




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2