2021年6月24日

[VBS script] 日期計算-幾天前或幾天後的日期

 

Ref: https://docs.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/dateadd-function

Ref :https://docs.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/datediff-function

ref: https://ryan-tw.blogspot.com/2012/11/blog-post.html

DateAdd(intervalnumberdate)
DateAdd 函數的語法有以下部分:



yyyy
q季度
m
y一年的日數
d
w一週的日數
ww
h小時
n分鐘
s


說明:

NewDate=DateAdd("d", -7, now())    "d":以天為單位;-7:往前7天;now():以目前日期為主

例:

2021/06/24 週四 15:55:51.67
d:\src\VBS\DateTime
> more DateTimeTest.vbs

time_now = Now()
gv3sec_before = DateAdd("s",-3,time_now)
gv7date_before = DateAdd("d",-7,time_now)
gv7date_after = DateAdd("d",7,time_now)

wScript.Echo "time_now = "&time_now
wScript.Echo "gv3sec_before = "&gv3sec_before
wScript.Echo "gv7date_before = "&gv7date_before
wScript.Echo "gv7date_after = "&gv7date_after

執行結果:

2021/06/24 週四 15:55:50.34
d:\src\VBS\DateTime
> cscript DateTimeTest.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corp. 1996-2006, 著作權所有,並保留一切權利

time_now = 2021/6/24 下午 03:55:51
gv3sec_before = 2021/6/24 下午 03:55:48
gv7date_before = 2021/6/17 下午 03:55:51
gv7date_after = 2021/7/1 下午 03:55:51


沒有留言: