PDA

View Full Version : vbscript datediff.vbs



IronBits
12-06-2008, 04:25 AM
If you don't enter an argument, it will take the current system date of your computer and change it from 'Sat 12/06/2008' and change it to 2008-12-06.

Assuming date /t on your computer gives you Sat 12/06/2008 ;)

cscript /nologo datediff.vbs 1 would show you 2008-12-05
cscript /nologo datediff.vbs 2 would show you 2008-12-04
cscript /nologo datediff.vbs 31 would show you 2008-11-05



' returns the date for how many days back you need to find the date for in YYYY-MM-DD format.

Option Explicit

Dim strNum, arrDate, strYear, strMonth, strDay, strDate
Dim objArgs

Set objArgs = WScript.Arguments

if WScript.Arguments.Count < 1 then
strNum = "0"
Else
strNum = Wscript.Arguments(0)
End If

arrDate = Split(DateAdd( "d" , -strNum , Date ), "/", -1, 1 )
strYear = arrDate(2)
strMonth = arrDate(0)
strDay = arrDate(1)
if strMonth < 10 then strMonth = "0"&strMonth
if strDay < 10 then strDay = "0"&strDay
strDate = strYear & "-" & strMonth & "-" & strDay

wscript.echo strDate
To use it in a DOS/CMD batchfile
for /f "tokens=*" %%a in ('cscript /nologo datediff.vbs 0') do set MyNewDate=%%a

echo %MyNewDate%
2008-12-06

WHY IS THE BB CODE tag broken ?????????? :cry:
HA! PHP code isn't broke :D

marshtric
12-24-2008, 07:48 PM
Required. The interval you want to use to calculate the differences between date1 and date2

Can take the following values:

* yyyy - Year
* q - Quarter
* m - Month
* y - Day of year
* d - Day
* w - Weekday
* ww - Week of year
* h - Hour
* n - Minute
* s - Second

IronBits
12-24-2008, 08:10 PM
Ah, fixed it moons ago, but thanks for the response. :)



' returns the date for how many days back you need
' to find the date for in YYYY-MM-DD format

Option Explicit

Dim strNum, arrDate, strYear, strMonth, strDay, strDate
Dim objArgs

Set objArgs = WScript.Arguments

if WScript.Arguments.Count < 1 then
strNum = "0"
Else
strNum = Wscript.Arguments(0)
End If

arrDate = Split(DateAdd( "d" , -strNum , Date ), "/", -1, 1 )
strYear = arrDate(2)
strMonth = arrDate(0)
strDay = arrDate(1)
if strMonth < 10 then strMonth = "0"&strMonth
if strDay < 10 then strDay = "0"&strDay
strDate = strYear & "-" & strMonth & "-" & strDay

wscript.echo strDate