按键精灵-自定义函数time(),datetime(),diff()
作者:互联网
函数1:diff(参数1,参数2,参数3)
参数1:时间间隔。(d天数/h小时/m分/s秒)
参数2:开始时间。
参数3:结束时间。
返回:数量。
函数2:dtime(参数)
参数:固定格式日期时间
返回:1970/1/1/0/0/0到指定日期时间所用秒数
函数3:add(参数1,参数2,参数3)
参数1:时间间隔。
参数2:时间数量(年、月不支持负数)。
参数3:开始时间。
返回:添加指定数量时间间隔的时间
函数4:ddate(参数)
参数:秒数
返回:固定格式日期时间。
下面是源码:
'添加指定时间日期
function add(jg,sl,datetime)
dim d,y,n,c,rq
rq=split(datetime,"/")
select case lcase(jg)
case "s"
d=sl
case "m"
d=sl*60
case "h"
d=sl*60*60
case "d"
d=sl*60*60*24
case "n"
y=int(rq(0)):n=int(rq(1)):d=0
While sl>12
if n<=2
if (y/4=int(y/4) and y/100<>int(y/100)) or (y/400=int(y/400) and y/100=int(y/100))
d=d+366*24*60*60
else
d=d+365*24*60*60
end if
elseif n>2
if ((y+1)/4=int((y+1)/4) and (y+1) /100<>int( (y+1) /100)) or ( (y+1) /400=int( (y+1) /400) and (y+1) /100=int( (y+1) /100))
d=d+366*24*60*60
else
d=d+365*24*60*60
end if
end if
sl=sl-12
y=y+1
Wend
for i=1 to sl+1
if n=1 or n=3 or n=5 or n=7 or n=8 or n=10 or n=12
c=31
elseif n=4 or n=6 or n=9 or n=11
c=30
elseif n=2
if (y/4=int(y/4) and y/100<>int(y/100) ) or (y/400=int(y/400) and y/100=int(y/100) )
c=29
else
c=28
end if
end if
if i<=sl
d=d+c*24*60*60
end if
if n=12
n=1
y=y+1
else
n=n+1
end if
next
if int(rq(2))>c
d=d-(int(rq(2))-c)*24*60*60
end if
case "y"
y=int(rq(0)):n=int(rq(1)):d=0
for sl
if n<=2
if (y/4=int(y/4) and y/100<>int(y/100)) or (y/400=int(y/400) and y/100=int(y/100))
d=d+366*24*60*60
else
d=d+365*24*60*60
end if
elseif n>2
if ((y+1)/4=int((y+1)/4) and (y+1)/100<>int((y+1)/100)) or ((1+y)/400=int((y+1)/400) and (1+y)/100=int((y+1)/100))
d=d+366*24*60*60
else
d=d+365*24*60*60
end if
end if
y=y+1
next
end select
add= ddate(int(dtime(datetime))+d)
end function
'两个时间差距
function diff(jg,dtime1,dtime2)
select case lcase(jg)
case "s"
diff=dtime(dtime2)-dtime(dtime1)
case "m"
diff=(dtime(dtime2)-dtime(dtime1))/60
case "h"
diff=(dtime(dtime2)-dtime(dtime1))/60/60
case "d"
diff=(dtime(dtime2)-dtime(dtime1))/60/60/24
end select
end function
'时间转换秒数
function dtime(datetime)
dim c,d
datetime=split(datetime,"/")
for i=0 to datetime(0)-1970-1
if ((1970+i)/4=int((1970+i)/4) and (1970+i)/100<>int((1970+i)/100)) or ((1970+i)/100=int((1970+i)/100) and (1970+i)/400=int((1970+i)/400) )
c=c+1
end if
next
d=(datetime(0)-1970)*365*24*60*60+c*24*60*60+datetime(3)*60*60+datetime(4)*60+datetime(5)
While int(datetime(1))>1
datetime(1)= datetime(1)-1
if datetime(1)=1 or datetime(1)=3 or datetime(1)=5 or datetime(1)=7 or datetime(1)=8 or datetime(1)=10 or datetime(1)=12
c=31
elseif datetime(1)=4 or datetime(1)=6 or datetime(1)=9 or datetime(1)=11
c=30
elseif datetime(1)=2 and (datetime(0)/4=int(datetime(0)/4) and datetime(0)/100<>int(datetime(0)/100)) or ( datetime(0)/400=int(datetime(0)/400) and datetime(0)/100=int(datetime(0)/100) )
c=29
else
c=28
end if
datetime(2)=datetime(2)+c
Wend
datetime(2)= datetime(2)-1
dtime=d+datetime(2)*24*60*60-8*60*60
end function
'秒转换日期时间
function ddate(time)
dim y,d,h,m,s,c,n
c=0
time=int(time)+8*60*60
y=int(time/(365*24*60*60))
for i=0 to int(y)-1
if ((1970+i)/4=int((1970+i)/4) and (1970+i)/100<>int((1970+i)/100)) or ((1970+i)/100=int((1970+i)/100) and (1970+i)/400=int((1970+i)/400) )
c=c+1
end if
next
y=y-int(c/365):c=0
for i=0 to int(y)-1
if ((1970+i)/4=int((1970+i)/4) and (1970+i)/100<>int((1970+i)/100)) or ((1970+i)/100=int((1970+i)/100) and (1970+i)/400=int((1970+i)/400) )
c=c+1
end if
next
c= 60*60*24*c
d=int((time-c-60*60*24*365*int(y))/60/60/24)
h=int((time-c-60*60*24*365*int(y)-int(d)*24*60*60)/60/60)
m=int((time-c-60*60*24*365*int(y)-int(d)*24*60*60-int(h)*60*60)/60)
s=(time-c-60*60*24*365*int(y)-int(d)*24*60*60-int(h)*60*60-int(m)*60)
c=31:n=1
While d>=c
d=d-c
n=n+1
if n=1 or n=3 or n=5 or n=7 or n=8 or n=10 or n=12
c=31
elseif n=4 or n=6 or n=9 or n=11
c=30
elseif n=2 and ((1970+y)/4=int((1970+y)/4) and (1970+y)/100<>int((1970+y)/100)) or ((1970+y)/100=int((1970+y)/100) and (1970+y)/400=int((1970+y)/400) )
c=29
else
c=28
end if
Wend
d=d+1
n=String(2-len(n),"0")&n
d= String(2-len(d),"0")&d
h= String(2-len(h),"0")&h
m= String(2-len(m),"0")&m
s= String(2-len(s),"0")&s
ddate= int(y+1970)&"/"&n&"/"&d&"/"&h&"/"&m&"/"&s
end function
标签:24,自定义,int,datetime,60,按键精灵,100,1970 来源: https://blog.csdn.net/biao197/article/details/101313954