当前位置: 移动技术网 > IT编程>脚本编程>VBScript > VBS日期(时间)格式化函数代码

VBS日期(时间)格式化函数代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

核心代码

currenttimestr1 = cstr(year(now()))&"-"&right("0"&month(now()),2)&"-"&right("0"&day(now()),2)&" "&right("0"&hour(now()),2)&":"&right("0"&minute(now()),2)&":"&right("0"&minute(now()),2)
currenttimestr2 = cstr(year(now()))&"-"&right("0"&month(now()),2)&"-"&right("0"&day(now()),2)

wscript.echo currenttimestr1 '2019-04-11 15:57:57
wscript.echo currenttimestr2 '2019-04-11

'格式化时间方法 n_flag(1-5)
wscript.echo format_time(now(),5)

function format_time(s_time, n_flag)
	dim y, m, d, h, mi, s
	format_time = ""
	if isdate(s_time) = false then exit function
	y = cstr(year(s_time))
	m = cstr(month(s_time))
	if len(m) = 1 then m = "0" & m
	d = cstr(day(s_time))
	if len(d) = 1 then d = "0" & d
	h = cstr(hour(s_time))
	if len(h) = 1 then h = "0" & h
	mi = cstr(minute(s_time))
	if len(mi) = 1 then mi = "0" & mi
	s = cstr(second(s_time))
	if len(s) = 1 then s = "0" & s
	select case n_flag
		case 1
			' yyyy-mm-dd hh:mm:ss
			format_time = y & "-" & m & "-" & d & " "& h &":" & mi &":" & s
		case 2
			' yyyy-mm-dd
			format_time = y & "-" & m & "-" & d
		case 3
			' hh:mm:ss
			format_time = h & ":" & mi & ":" & s
		case 4
			' yyyy年mm月dd日
			format_time = y & "年" & m & "月" & d & "日"
		case 5
			' yyyymmdd
			format_time = y & m & d
	end select
end function

vbscript下格式化时间和日期的函数

我们有时候遇到的日期格式可能是2020-1-12   ,系统自动将月份中的0去掉了,但是有时候我们需要完整的日期格式 ,如:2020-01-12  那么怎么办呢?下面的几个函数可以轻松搞定

'将一个一位的数字前面加零
function fillzero(str)
   ttt=str
   if len(str)=1 then
      ttt="0" & str
   end if
   fillzero=ttt
end function

'转化日期,将 一位补上零  2003-1-2  -->  2003-01-02
function convertdate(tdate)
   ttt=tdate
   if isdate(tdate) then
      ttt=year(tdate) & "-" & fillzero(month(tdate)) & "-" & fillzero(day(tdate))
   end if
   convertdate=ttt
end function

'输入一个日期时间串,转换成年四位,其他两位的新的日期时间串
function convertdatetime(tdatetime)
   ttt=tdatetime
   if isdate(tdatetime) then
      ttt=year(tdatetime) & "-" & fillzero(month(tdatetime)) & "-" & fillzero(day(tdatetime)) & " " & fillzero(cstr(hour(tdatetime))) & ":" & fillzero(cstr(minute(tdatetime))) & ":" & fillzero(cstr(second(tdatetime)))
   end if
   convertdatetime=ttt
end function

这篇文章就介绍到这了,需要的朋友可以参考一下。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网