51Testing软件测试论坛
标题:
提供一个处理日期的函数-VBScript的
[打印本页]
作者:
javaleo
时间:
2007-5-21 21:57
标题:
提供一个处理日期的函数-VBScript的
QTP是用VBScript的,而VBScript里面关于日期格式的过程很少,这里有一个过程,可以得到各种日期格式
function formatDate(format, intTimeStamp)
Dim monthname()
Redim monthname(12)
monthname(1) = "January"
monthname(2) = "February"
monthname(3) = "March"
monthname(4) = "April"
monthname(5) = "May"
monthname(6) = "June"
monthname(7) = "July"
monthname(8) = "August"
monthname(9) = "September"
monthname(10) = "October"
monthname(11) = "November"
monthname(12) = "December"
dim unUDate, A
dim OriginalLocale
dim res
OriginalLocale = GetLocale
res = SetLocale("en-gb")
' Test to see if intTimeStamp looks valid. If not, they have passed a normal date
if not (isnumeric(intTimeStamp)) then
if isdate(intTimeStamp) then
intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
else
response.write "Date Invalid"
exit function
end if
end if
if (intTimeStamp=0) then
unUDate = now()
else
unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
end if
unUDate = trim(unUDate)
'bug fix for midnight problems
If (Len(unUDate) <= 11) Then unUDate = Trim(unUDate) & " 00:00:00"
dim startM : startM = 1
dim startD : startD = InStr(startM, unUDate, "/")+1
dim startY : startY = InStr(startD, unUDate, "/")+1
dim startHour : startHour = InStr(startY, unUDate, " ")+1
dim startMin : startMin = InStr(startHour, unUDate, ":")+1
dim startSec : startSec = InStr(startMin+1, unUDate, ":")+1
dim dateMonth : dateMonth = mid(unUDate, startD, ((startY - 1) - startD))
dim dateDay : dateDay = mid(unUDate, 1, ((startD - 1) - 1))
dim dateYear : dateYear = Year(unUDate)
dim dateHour : dateHour = mid(unUDate, startHour, ((startMin - startHour) - 1))
dim dateMinute : dateMinute = mid(unUDate, startMin, 2)
dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":") + 1, 2)
format = replace(format, "%Y", right(dateYear, 4))
format = replace(format, "%y", right(dateYear, 2))
format = replace(format, "%m", dateMonth)
format = replace(format, "%n", cint(dateMonth))
' Response.Write CStr(cint(dateMonth))
' Response.Flush
format = replace(format, "%F", monthname(cint(dateMonth)))
format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
format = replace(format, "%d", dateDay)
format = replace(format, "%j", cint(dateDay))
format = replace(format, "%h", mid(unUDate, startHour, 2))
format = replace(format, "%g", cint(mid(unUDate, startHour, 2)))
if (cint(dateHour) > 12) then
A = "PM"
else
A = "AM"
end if
format = replace(format, "%A", A)
format = replace(format, "%a", lcase(A))
if (A = "PM") then format = replace(format, "%H", Right("00" & dateHour - 12, 2))
format = replace(format, "%H", dateHour)
if (A = "PM") then format = replace(format, "%G", left("0" & cint(dateHour) - 12, 2))
format = replace(format, "%G", cint(dateHour))
format = replace(format, "%i", dateMinute)
format = replace(format, "%I", cint(dateMinute))
format = replace(format, "%s", dateSecond)
format = replace(format, "%S", cint(dateSecond))
format = replace(format, "%L", WeekDay(unUDate))
format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3))
format = replace(format, "%l", WeekDayName(WeekDay(unUDate)))
format = replace(format, "%U", intTimeStamp)
format = replace(format, "11%O", "11th")
format = replace(format, "1%O", "1st")
format = replace(format, "12%O", "12th")
format = replace(format, "2%O", "2nd")
format = replace(format, "13%O", "13th")
format = replace(format, "3%O", "3rd")
format = replace(format, "%O", "th")
formatDate = format
res = SetLocale(OriginalLocale)
end function
复制代码
用法
eg: strDateTime = formatDate("%g:%i%a, %l %j%O %F, %Y", UDate(Now()))
%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)
复制代码
作者:
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