Results 1 to 3 of 3

Thread: vbscript datediff.vbs

  1. #1
    Target Butt IronBits's Avatar
    Join Date
    Dec 2001
    Location
    Morrisville, NC
    Posts
    8,619

    vbscript datediff.vbs

    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

    PHP Code:
    ' 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 ??????????
    HA! PHP code isn't broke

  2. #2
    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

  3. #3
    Target Butt IronBits's Avatar
    Join Date
    Dec 2001
    Location
    Morrisville, NC
    Posts
    8,619
    Ah, fixed it moons ago, but thanks for the response.

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

    Option Explicit

    Dim strNum
    arrDatestrYearstrMonthstrDaystrDate
    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)
    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 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •