VBS_简单实例

这篇文章来记录一些用 vbscript 编写的实例。

大部分实例是内嵌在 html 中的脚本,剩余是 windows 下的脚本。

显示日期和时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is " & date())
document.write("<br>")
document.write("The time is " & time())
</script>
</body>
</html>

<!--
运行结果:
Today's date is 2022/11/15
The time is 13:26:56
-->

上面的代码中,包含了两个 vbs 自带的函数:date()time(),一个返回日期,一个返回时间。

显示星期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html>
<body>
<p>VBScripts' function <b>WeekdayName</b> is used to get a weekday:</p>
<script type="text/vbscript">
document.write("<p>")
document.write(weekdayname(1))
document.write("<br>")
document.write(weekdayname(2))
document.write("</p><p>")

document.write("Get the abbreviated name of a weekday:")
document.write("<br>")
document.write(weekdayname(1, true))
document.write("<br>")
document.write(weekdayname(2, true))
document.write("</p><p>")

document.write("Get the current weekday:")
document.write("<br>")
document.write(weekdayname(weekday(date)))
document.write("<br>")
document.write(weekdayname(weekday(date), true))
document.write("</p>")
</script>
</body>
</html>

<!--
运行结果:
VBScripts' function WeekdayName is used to get a weekday:

星期日
星期一

Get the abbreviated name of a weekday:
周日
周一

Get the current weekday:
星期二
周二
-->

注意weekdayname函数的返回会根据是否提供第二个参数而变化。

显示月份和星期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
document.write("Today's day is " & weekdayname(weekday(date)))
document.write("<br>")
document.write("The month is " &monthName(month(date)))
</script>
</body>
</html>

<!--
运行结果:
Today's day is 星期二
The month is 十一月
-->

计算日期差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html>
<body>
<p>Countdown to year 3000:</p>
<p>
<script type="text/vbscript">
millennium=CDate("1/1/3000 00:00:00")
document.write(millennium & "<br>")
document.write("It is " & DateDiff("yyyy", Now(), millennium) & " years to year 3000!<br>")
document.write("It is " & DateDiff("m", Now(), millennium) & " months to year 3000!<br>")
document.write("It is " & DateDiff("d", Now(), millennium) & " days to year 3000!<br>")
document.write("It is " & DateDiff("h", Now(), millennium) & " hours to year 3000!<br>")
document.write("It is " & DateDiff("n", Now(), millennium) & " minutes to year 3000!<br>")
document.write("It is " & DateDiff("s", Now(), millennium) & " seconds to year 3000!<br>")
</script>
</p>
</body>
</html>
<!--
运行结果:
Countdown to year 3000:

3000/1/1
It is 978 years to year 3000!
It is 11726 months to year 3000!
It is 356889 days to year 3000!
It is 8565322 hours to year 3000!
It is 513919281 minutes to year 3000!
It is 30835156829 seconds to year 3000!

-->

cdate函数用来生成一个日期,其中包含日期和具体时间;now函数用来返回当前日期和时间;datediff函数用来计算日期和时间之差。

计算日期之和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
document.write(DateAdd("d", 0, date()))
document.write("<br>")
document.write(DateAdd("d", 30, date()))
</script>
</body>
</html>
<!--
运行结果:
Tue Nov 15 00:00:00 UTC+0800 2022
Thu Dec 15 00:00:00 UTC+0800 2022
-->

dateadd用来计算日期之和,用法与datediff类似。

按照不同格式输出日期/时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
document.write(formatdatetime(date(), vbgeneraldate))
document.write("<br>")
document.write(formatdatetime(date(), vblongdate))
document.write("<br>")
document.write(formatdatetime(date(), vbshortdate))
document.write("<br>")
document.write(formatdatetime(date(), vblongtime))
document.write("<br>")
document.write(formatdatetime(date(), vbshorttime))
</script>
<p>Syntax for formatdatetime: formatdatetime(date, nameformat).</p>
</body>
</html>

<!--
运行结果:
2022/11/15
2022年11月15日
2022/11/15
0:00:00
00:00
Syntax for formatdatetime: formatdatetime(date, nameformat).
-->

formatdatetime函数可以根据不同的格式输出日期,第一个参数为日期,第二个参数为格式类型。

大小写转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
txt = "Have a nice day!"
document.write(txt & "<br>")
document.write(ucase(txt))
document.write("<br>")
document.write(lcase(txt))
</script>
</body>
</html>
<!--
Have a nice day!
HAVE A NICE DAY!
have a nice day!
-->

ucase函数转换为大写,lcase转换为小写。

删除字符串两端的空格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
fname = " Bill "
document.write("Hello" & Trim(fname) & "Gates<br>")
document.write("Hello" & RTrim(fname) & "Gates<br>")
document.write("Hello" & LTrim(fname) & "Gates<br>")
</script>
</body>
</html>

<!--
HelloBillGates
Hello BillGates
HelloBill Gates
-->

Trim函数默认删除字符串两端的空格,RTrim默认删除字符串右端的空格,LTrim默认删除字符串左端的空格。
PS:document.write在显示字符串时,开头不显示,结尾默认只显示一个。

逆置字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
txt = "Hello Everyone!"
document.write(txt & "<br>")
document.write(strreverse(txt))
</script>
</body>
</html>

<!--
运行结果:
Hello Everyone!
!enoyrevE olleH
-->

跟 C++ 中的 reverse 函数一样,没什么好说的。

取整函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
i = 48.66776677
j = 48.33333333
document.write(round(i))
document.write("<br>")
document.write(round(j))
</script>
</body>
</html>
<!--
运行结果:
49
48
-->

跟 C/C++ 的round函数功能类似,默认四舍五入。

返回随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
randomize()
document.write(rnd())
</script>
</body>
</html>
<!--
运行结果:
0.3182794451713562
-->

randomize函数用于产生随机数种子,rnd函数用于返回一个随机数,数字总是小于 1 且大于等于 0。

产生 [0, 99] 的随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
randomize()
random_number = int(100 * rnd())
document.write("A random number: <b>" & random_number & "</b>")
</script>
</body>
</html>

<!--
运行结果:
A random number: 37
-->

从字符串的左侧或右侧返回指定数目的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
txt = "Welcome to our web site!"
document.write(left(txt, 5))
document.write("<br>")
document.write(right(txt, 5))
</script>
</body>
</html>

<!--
运行结果:
Welco
site!
-->

替换字符串中的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
txt = "Welcome to this web!"
document.write(txt & "<br>")
document.write(replace(txt, "web", "page"))
</script>
</body>
</html>

<!--
运行结果:
Welcome to this web!
Welcome to this page!
-->

从一段字符串指定位置返回指定数目的字符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<body>
<script type="text/vbscript">
txt = "Welcome to our web site!"
document.write(txt & "<br>")
document.write(mid(txt, 9, 2))
</script>
</body>
</html>
<!--
运行结果:
Welcome to our web site!
to
-->

有点类似 C++ string 类的substr函数。


Buy me a coffee ? :)
0%