Sign in to follow this  
Followers 0
GWmellon

Subtract date

4 posts in this topic

I did search but didn't find anything useful. I need to fetch schedule files (yearmonthdate.schedule) from today backwards for the last seven days. I found _Dateadd but there is not a _Datesubtract. Obviously, @MDAY -1 won't work.

Any suggestions? The best I have come up with is using _DateDaysInMonth in a function to decide what needs to be done. It is clunky as hell.

Share this post


Link to post
Share on other sites



Here is _dateadd:

; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
; Language:       English
; Description:    Functions that assist with dates and times.
;
;===============================================================================
;
; Description:      Calculates a new date based on a given date and add an interval.
; Parameter(s):     $sType    D = Add number of days to the given date
;                             M = Add number of months to the given date
;                             Y = Add number of years to the given date
;                             w = Add number of Weeks to the given date
;                             h = Add number of hours to the given date
;                             n = Add number of minutes to the given date
;                             s = Add number of seconds to the given date
;                   $iValToAdd - number to be added
;                   $sDate    - Input date in the format YYYY/MM/DD[ HH:MM:SS]
; Requirement(s):   None
; Return Value(s):  On Success - Date newly calculated date.
;                   On Failure - 0  and Set
;                                   @ERROR to:  1 - Invalid $sType
;                                                  2 - Invalid $iValToAdd
;                                                  3 - Invalid $sDate
; Author(s):        Jos van der Zande
; Note(s):          The function will not return an invalid date.
;                   When 3 months is are to '2004/1/31' then the result will be 2004/04/30
;
;
;===============================================================================
Func _DateAdd($sType, $iValToAdd, $sDate)
    Local $asTimePart[4]
    Local $asDatePart[4]
    Local $iJulianDate
    Local $iTimeVal
    Local $iNumDays
    Local $Day2Add
    ; Verify that $sType is Valid
    $sType = StringLeft($sType, 1)
    If StringInStr("D,M,Y,w,h,n,s", $sType) = 0 Or $sType = "" Then
        SetError(1)
        Return (0)
    EndIf
    ; Verify that Value to Add  is Valid
    If Not StringIsInt($iValToAdd) Then
        SetError(2)
        Return (0)
    EndIf
    ; Verify If InputDate is valid
    If Not _DateIsValid($sDate) Then
        SetError(3)
        Return (0)
    EndIf
    ; split the date and time into arrays
    _DateTimeSplit($sDate, $asDatePart, $asTimePart)
    
    ; ====================================================
    ; adding days then get the julian date
    ; add the number of day
    ; and convert back to Gregorian
    If $sType = "d" Or $sType = "w" Then
        If $sType = "w" Then $iValToAdd = $iValToAdd * 7
        $iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $iValToAdd
        _DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
    EndIf
    ; ====================================================
    ; adding Months
    If $sType = "m" Then
        $asDatePart[2] = $asDatePart[2] + $iValToAdd
        ; pos number of months
        While $asDatePart[2] > 12
            $asDatePart[2] = $asDatePart[2] - 12
            $asDatePart[1] = $asDatePart[1] + 1
        WEnd
        ; Neg number of months
        While $asDatePart[2] < 1
            $asDatePart[2] = $asDatePart[2] + 12
            $asDatePart[1] = $asDatePart[1] - 1
        WEnd
    EndIf
    ; ====================================================
    ; adding Years
    If $sType = "y" Then
        $asDatePart[1] = $asDatePart[1] + $iValToAdd
    EndIf
    ; ====================================================
    ; adding Time value
    If $sType = "h" Or $sType = "n" Or $sType = "s" Then
        $iTimeVal = _TimeToTicks($asTimePart[1], $asTimePart[2], $asTimePart[3]) / 1000
        If $sType = "h" Then $iTimeVal = $iTimeVal + $iValToAdd * 3600
        If $sType = "n" Then $iTimeVal = $iTimeVal + $iValToAdd * 60
        If $sType = "s" Then $iTimeVal = $iTimeVal + $iValToAdd
        ; calculated days to add
        $Day2Add = Int($iTimeVal/ (24 * 60 * 60))
        $iTimeVal = $iTimeVal - $Day2Add * 24 * 60 * 60
        If $iTimeVal < 0 Then
            $Day2Add = $Day2Add - 1
            $iTimeVal = $iTimeVal + 24 * 60 * 60
        EndIf
        $iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $Day2Add
        ; calculate the julian back to date
        _DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
        ; caluculate the new time
        _TicksToTime($iTimeVal * 1000, $asTimePart[1], $asTimePart[2], $asTimePart[3])
    EndIf
    ; ====================================================
    ; check if the Input day is Greater then the new month last day.
    ; if so then change it to the last possible day in the month
    $iNumDays = StringSplit('31,28,31,30,31,30,31,31,30,31,30,31', ',')
    If _DateIsLeapYear($asDatePart[1]) Then $iNumDays[2] = 29
    ;
    If $iNumDays[$asDatePart[2]] < $asDatePart[3] Then $asDatePart[3] = $iNumDays[$asDatePart[2]]
    ; ========================
    ; Format the return date
    ; ========================
    ; Format the return date
    $sDate = $asDatePart[1] & '/' & StringRight("0" & $asDatePart[2], 2) & '/' & StringRight("0" & $asDatePart[3], 2)
    ; add the time when specified in the input
    If $asTimePart[0] > 0 Then
        If $asTimePart[0] > 2 Then
            $sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2) & ':' & StringRight("0" & $asTimePart[3], 2)
        Else
            $sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2)
        EndIf
    EndIf
    ;
    Return ($sDate)
EndFunc   ;==>_DateAdd

It should be easily modified to become _dateminus

Share this post


Link to post
Share on other sites

I did search but didn't find anything useful. I need to fetch schedule files (yearmonthdate.schedule) from today backwards for the last seven days. I found _Dateadd but there is not a _Datesubtract. Obviously, @MDAY -1 won't work.

Any suggestions? The best I have come up with is using _DateDaysInMonth in a function to decide what needs to be done. It is clunky as hell.

Hi, just use neg number;

; _DateAdd2.au3
#include <Date.au3>
#include <array.au3>
local $ar_Answers[7][2]
for $i= 1 to 7
    $ar_Answers[$i-1][0]=$i&" days back"
    $ar_Answers[$i-1][1]=_DateAdd( 'd',$i, _NowCalcDate())
Next
_ArrayDisplay($ar_Answers,"$ar_Answers")
Best, randall

Share this post


Link to post
Share on other sites

Hi, just use neg number;

; _DateAdd2.au3
#include <Date.au3>
#include <array.au3>
local $ar_Answers[7][2]
for $i= 1 to 7
    $ar_Answers[$i-1][0]=$i&" days back"
    $ar_Answers[$i-1][1]=_DateAdd( 'd',$i, _NowCalcDate())
Next
_ArrayDisplay($ar_Answers,"$ar_Answers")
Best, randall
Thanks. This is a fine example of "Read the entire help page you dope!" on my part. Just noticed this in the examples

; Subtract 2 weeks from today
$sNewDate = _DateAdd( 'w',-2, _NowCalcDate())
MsgBox( 4096, "", "Today minus 2 weeks: " & $sNewDate )

Didn't realise you could use a neg until you pointed out. Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0